This is an old revision of the document!
Using Node.js
Hello world (text mode):
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; // Text mode: res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Hello World (HTML mode):
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; // HTML mode: res.setHeader('Content-Type', 'text/html'); res.write("Requested URL: " + req.url); res.write("<h3>Phone Logs</h3>"); res.write("<a href='/data/report_01-02-2017.htm'>report_01-02-2017</a>"); // url of file, relative to root of http server res.end(); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Read requested files:
const http = require('http'); const fs = require('fs'); const path = require('path'); const url = require('url'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; // Read requested file fs.readFile('./data/report_01-02-2017.htm', function(err, data) { // relative path of file res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); res.end(); }); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Serve Homepage and subpages:
const http = require('http'); const fs = require('fs'); const path = require('path'); const url = require('url'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; var q = url.parse(req.url, true); var filename = "." + q.pathname; switch(q.pathname) { case '/': // Serve Homepage res.setHeader('Content-Type', 'text/html'); res.write("Requested URL: " + req.url); res.write("<h3>Phone Logs</h3>"); //res.write("<a href='/data/report_01-02-2017.htm'>report_01-02-2017</a>"); // url of file, relative to root of http server // Read file listing var pth = "./data"; fs.readdir(pth, function (err, files) { if (err) { throw err; } files.map(function (file) { return path.join(pth, file); }).filter(function (file) { //return fs.statSync(file).isFile(); return fs.stat(file).isFile(); }).forEach(function (file) { console.log("%s (%s)", file, path.extname(file)); res.write("<pre>%s (%s)</pre>\n", file, path.extname(file)); }); }); res.end(); break; default: // Read and serve requested file, eg. '/data/report_01-02-2017.htm' fs.readFile(filename, function(err, data) { if (err) { res.writeHead(404, {'Content-Type': 'text/html'}); return res.end("404 Not Found"); } res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); return res.end(); }); break; } }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Running App Forever
Install forever
module
$ npm install -g forever
And then start your application with:
$ forever server.js
Or as a service:
$ forever start server.js
Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:
$ forever -m5 server.js
To list all running processes:
$ forever list
Note the integer in the brackets and use it as following to stop a process:
$ forever stop 0
Restarting a running process goes:
$ forever restart 0
If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:
$ forever -w server.js