How to load and run js file automatically when the server starts


I am a noob of NodeJS and RedwoodJS. I have a question regarding when and how the JS files under api/src/lib are loaded and run. For example, code in auth.ts and db.ts can be loaded and run automatically when the server starts (when I perform yarn rw dev). If I want to add a new file, e.g., datadog.ts, under api/src/lib/, how do I make the code there loaded and run automatically?

Many thanks

In Redwood, each file in the functions folder is an entrypoint (each one is hosted as a separate serverless function). The default one is graphql.ts, where it defines your graphQL server and connects to all of your services. If you want to load a specific file, you can import it in that file and it’ll run, but you generally want to export functions/objects to be run in a specific service (what gets executed on a GraphQL call).

Hope this helps!

Thanks for your reply. I basically want to start a datadog RUM monitoring service that runs when my web app starts to run. In its document I see the following instructions to enable datadog RUM for npm: RUM Browser Monitoring. Should I add a separate file under the src/functions folder then paste the above code snippet into it?

NVM. This monitoring instance should be installed at frontend rather than backend …

If I need to run any monitoring instance at backend, should I add a separate serverless function?

It’s an option, I’m not familiar with monitoring best practices though :slight_smile:

Hi. May I ask

  1. How and where do you intend to deploy your app?
  2. What metrics are you interested it collecting and analyzing in Datadog and for what purpose — what questions are you looking to answer?

I ask because there may be other ways to do what you want to achieve if you can describe what that is.

The Core Tean is working on some observability features and your use case can help the team see if what we may be adding helps.