Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:js:using_node.js [2018/07/13 16:09] smayr |
swdev:js:using_node.js [2018/07/18 14:03] (current) smayr [References] |
||
---|---|---|---|
Line 21: | Line 21: | ||
Hello World (HTML mode): | Hello World (HTML mode): | ||
- | < | + | < |
const http = require(' | const http = require(' | ||
const hostname = ' | const hostname = ' | ||
Line 135: | Line 135: | ||
</ | </ | ||
- | === Running App Forever | + | == Running App Forever == |
- | Install '' | + | Install '' |
$ npm install -g forever | $ npm install -g forever | ||
- | And then start your application | + | Start your application: |
$ forever server.js | $ forever server.js | ||
- | Or as a service: | + | Alternatively, |
$ forever start server.js | $ forever start server.js | ||
- | Forever | + | '' |
$ forever -m5 server.js | $ forever -m5 server.js | ||
- | To list all running processes: | + | List all running processes, note the integer in the brackets, and use it to stop a process: |
$ forever list | $ forever list | ||
- | |||
- | Note the integer in the brackets and use it as following to stop a process: | ||
$ forever stop 0 | $ forever stop 0 | ||
- | Restarting | + | List all running processes, and restart |
+ | $ forever list | ||
$ forever restart 0 | $ forever restart 0 | ||
- | If you' | + | When working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes: |
$ forever -w server.js | $ forever -w server.js | ||
+ | == Running App as Cronjob == | ||
+ | <code javascript> | ||
+ | // Run main loop as a cronjob. | ||
+ | // Alternative to: $ forever start app.js (requires: $ npm install forever) | ||
+ | var CronJob = require(' | ||
+ | var job = new CronJob({ | ||
+ | cronTime: '00 30 11 * * 1-5', | ||
+ | onTick: function() { | ||
+ | /* | ||
+ | * Runs every weekday (Monday through Friday) | ||
+ | * at 11:30:00 AM. It does not run on Saturday | ||
+ | * or Sunday. | ||
+ | */ | ||
+ | }, | ||
+ | start: false, | ||
+ | timeZone: ' | ||
+ | }); | ||
+ | job.start(); | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | // Run main loop, executing doSomeWork() on timer | ||
+ | // Alternative to: $ forever start app.js (requires: $ npm install forever) | ||
+ | function run() { | ||
+ | setInterval(doSomeWork, | ||
+ | }; | ||
+ | |||
+ | run(); | ||
+ | </ | ||
== References == | == References == | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// |