2

I need help to create a few node js servers. One server should read from aws dynamodb (like once every minute), one should read data from a api, like once every day. Like cronjobs.

My problem here is how I should write an application that is running 24/7. Iam guessing while(true){doStuff()} is wrong?

1
  • I know this is old question, if someone need help in 2021, nice explanation video is there : youtu.be/KEZdvF1sADg Commented Aug 17, 2021 at 14:57

2 Answers 2

3

You can simply run your node.js script as a daemon using forever

and then you can use setInterval to run a js code every x seconds.

Another approach is really using cronjobs that run the scripts you want every minute, whatever suits your needs better :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I was looking for.
2

I would suggest a zero downtime production process manager called pm2. I run my nodejs server using pm2 in my company, and I didn't face any downtime in 3 years.

Microsoft, paypal, IMB uses pm2. Check here.

Using this module is dead simple:

Step 1:

npm install pm2 -g

Step 2: cd to main file of nodejs. app.js is this file(or www)

pm2 start app.js

That's it. Once set up, your server will run without any zero downtime. Make sure you hit

pm2 list

to cross check if your application has correctly started.

2 Comments

Thanks, looks very nice. Maybe overkill for me, don´t know yet, but i will try it out.
Its dead-simple to use. Check my update. pm2 was primarily built for node js. Remember nodejs is a single-threaded app. If you code catch any exception, none of the http request will be listened and your server will be down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.