Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:js:using_node.js [2017/10/26 17:37]
smayr [References]
swdev:js:using_node.js [2018/07/18 14:03] (current)
smayr [References]
Line 2: Line 2:
  
 Hello world (text mode): Hello world (text mode):
-<code js>+<code javascript>
 const http     = require('http'); const http     = require('http');
 const hostname = '127.0.0.1'; const hostname = '127.0.0.1';
Line 21: Line 21:
  
 Hello World (HTML mode): Hello World (HTML mode):
-<code js>+<code javascript>
 const http     = require('http'); const http     = require('http');
 const hostname = '127.0.0.1'; const hostname = '127.0.0.1';
Line 44: Line 44:
  
 Read requested files: Read requested files:
-<code js>+<code javascript>
 const http = require('http'); const http = require('http');
 const fs   = require('fs'); const fs   = require('fs');
Line 69: Line 69:
  
 Serve Homepage and subpages: Serve Homepage and subpages:
-<code js>+<code javascript>
 const http = require('http'); const http = require('http');
 const fs   = require('fs'); const fs   = require('fs');
Line 135: Line 135:
 </code> </code>
  
 +== Running App Forever ==
 +Install ''forever'' module:
 +  $ npm install -g forever
 +
 +Start your application:
 +  $ forever server.js
 +
 +Alternatively, start your application as a service:
 +  $ forever start server.js
 +
 +''forever'' restarts your app when it crashes or stops for some reason. To restrict restarts to 5 times, use:
 +  $ forever -m5 server.js
 +
 +List all running processes, note the integer in the brackets, and use it to stop a process:
 +  $ forever list
 +  $ forever stop 0
 +
 +List all running processes, and restart a running process goes:
 +  $ forever list
 +  $ forever restart 0
 +
 +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
 +
 +== 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('cron').CronJob;
 +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: 'America/New_York'
 +});
 +job.start();
 +</code>
 +
 +<code javascript>
 +// Run main loop, executing doSomeWork() on timer
 +// Alternative to: $ forever start app.js (requires: $ npm install forever)
 +function run() {
 +    setInterval(doSomeWork, refreshInterval);
 +};
 +
 +run();
 +</code>
 == References == == References ==
   * [[https://www.w3schools.com/nodejs/default.asp|W3 Schools Tutorial: Node.js]]   * [[https://www.w3schools.com/nodejs/default.asp|W3 Schools Tutorial: Node.js]]
   * [[https://nodejs.org/dist/latest-v6.x/docs/api/synopsis.html|Node.js API Synopsis]]   * [[https://nodejs.org/dist/latest-v6.x/docs/api/synopsis.html|Node.js API Synopsis]]
   * [[http://nodeexamples.com|Node Examples]]   * [[http://nodeexamples.com|Node Examples]]
 +  * [[https://github.com/mapbox/node-sqlite3/wiki/API|SQLite3 API for Node JS]]