简体   繁体   English

如何实现需要持续运行的节点应用程序?

[英]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.我已经用 node 和 phantom.js 构建了一个简单的应用程序(循环),它会在网络上抓取一些数据,并定期向我发送新结果的电子邮件。

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,但是我应该从那里创建一个简单的仪表板来启动和停止我的网络爬虫循环吗? Heroku needs to spin up after like an hour of inactivity so that might not work. Heroku 需要在大约一个小时的不活动后启动,所以这可能不起作用。

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 :)我认为PM2是答案:)

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.我从未使用过 Heroku,但您可以创建一个快速服务器并使用 Ngrok 公开公开该应用程序。

You can create an html page and access the app via Ngrok that way.您可以创建一个 html 页面并通过 Ngrok 访问该应用程序。

Here is a link: https://ngrok.com/这是一个链接: https : //ngrok.com/

Quick overview:快速概览:
1) Create and run a local express server 1) 创建并运行本地 express 服务器
2) run ngrok tunnel service (ngrok http server port ) 2)运行ngrok隧道服务(ngrok http服务器端口
3) ngrok will then respond with a url, kinda looks like a hash code, which you can use to access your app from anywhere 3) 然后 ngrok 会回复一个 url,有点像一个哈希码,你可以用它从任何地方访问你的应用程序

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.我不确定 ngrok 公共服务器会存活多久,但根据我的经验,只要我运行进程,它就会一直运行。

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.其他选项是直接将应用程序托管在 AWS 等 Web 服务上,并公开端点 - 但如果您不熟悉,则需要进行一些学习。

You could use Glitch for hosting.您可以使用Glitch进行托管。 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).它是免费的,不会去任何地方(所以你不必担心它只是一个临时解决方案),并且非常适合 Node 应用程序,尽管有一些技术限制(你需要设置一些自动ping 你的应用程序,只是为了让它运行)。

Just set up a webpage using something like express with a couple of inputs and start/stop buttons.只需使用带有几个输入和开始/停止按钮的express 之类的东西来设置网页。 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.这些会将请求发送到服务器(GET 请求可能是最容易使用的),然后当您单击开始时,服务器可以使用您的输入作为参数运行函数,当您单击停止时,它可以以某种方式取消该函数或只是终止节点进程。

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. PM2 和 Forever 之类的东西会在窗口关闭时让您的应用程序保持运行,但除非涉及某种魔法,否则如果您的计算机关闭,据我所知,它们不会有太大作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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