Next.js Discord

Discord Forum

How to run code on server startup ?

Unanswered
Mathissou posted this in #help-forum
Open in Discord
I'd like to setup my database once in the server startup, to avoid checking if tables exists etc for every queries. How to ?

13 Replies

@Coffee Coke if you mean running production server locally use `next start`
I want to execute a code as soon as the server starts
and not execute them anymore
you can execute some code before and after but I don't know how you could execute as soon as the server starts
@Coffee Coke you can execute some code before and after but I don't know how you could execute as soon as the server starts
the thing is that i want to check if the table users exists before to query data in it. I don't want to check it on every queries tho
only once, when the server starts
@Mathissou the thing is that i want to check if the table users exists before to query data in it. I don't want to check it on every queries tho
do you want to implement your checking logic inside the project or can be done with different script?
I would run the script for checking if users table exists and start the server after
@Coffee Coke do you want to implement your checking logic inside the project or can be done with different script?
well if the script is inside the project, that would be better ig
kinda barbaric to make different scripts for those things, isn't it ?
@Mathissou well if the script is inside the project, that would be better ig
I don't know how is that possible, sorry
West African Lion
If you're using output: "standalone" using node's -r flag would also work

https://nodejs.org/api/cli.html#-r---require-module

node -r script.js server.js