简体   繁体   中英

How can I implement my node application that needs to be constantly running?

I have built a simple application (loop) with node and phantom.js that scrapes the web for some data and emails me periodically with new results.

Currently I run the command in my local terminal and as long as the window is open it will continue to run. My question is how should I go about implementing an interface (web app most likely) so that I can type in my parameters and have it run even when I leave the page?

I've heard talk of a package called 'forever' but I'm unsure of how it works if maybe someone could explain that to me.

I have my own shared hosting but conceptually I'm confused about how I could implement this. I can push my code to heroku but from there should I maybe create a simple dashboard for me to start and stop my web scraper loop? Heroku needs to spin up after like an hour of inactivity so that might not work.

My app currently just picks up the parameters from the command line so I'd obviously have to instead grab the data from input fields or something.

High level concept of what needs to be done would be great help.

I think PM2 is the answer :)

You can manage your node app in your system for instance you can start a node project:

pm2 start app.js

and for listing your current process

pm2 list

and so on ...

I have never used Heroku, but you can create an express server and publicly expose the application using Ngrok.

You can create an html page and access the app via Ngrok that way.

Here is a link: https://ngrok.com/

Quick overview:
1) Create and run a local express server
2) run ngrok tunnel service (ngrok http server port )
3) ngrok will then respond with a url, kinda looks like a hash code, which you can use to access your app from anywhere

I am not sure how long ngrok public server will stay alive for though, in my experience it has stayed on as long as I have the process running.

Other options are straight up hosting the application on a web service such as AWS, and exposing endpoints - but if you are not familiar with that there is some learning involved.

You could use Glitch for hosting. It's free, isn't going anywhere (so you don't need to worry about it only being a temporary solution), and works great for Node applications, although there are some technical restrictions (you'd need to set something up to automatically ping your application, just to keep it running).

Just set up a webpage using something like express with a couple of inputs and start/stop buttons. Those would sent requests to the server (GET requests would probably be the easiest to work with) which could then run functions with your inputs as the arguments when you click start, and when you click stop it could either cancel the function somehow or just kill the node process.

That way you should able to run your app for a (theoretically) infinite amount of time, even if your computer is off. Things like PM2 and Forever will keep your app running while the window is closed, but unless there's some sort of magic involved they won't do much if your computer is turned off as far as I know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM