đź“ť Edit page
âž• Add page
Web servers
Start a server
The general ways to start a server.
Node
Run as:
$ node server.js
NPM
This will lookup server.js
by default.
$ npm serve
HTTP
Using the Node builtin module.
See HTTP module docs.
Basic
Example based on Getting started with NodeJS for frontend developers - Part 1.
Set up HTTP server with returns a greeting as plain text.
server.js
const http = require('http') http.createServer((req, resp) => { resp.writeHead(200, { 'Content-Type': 'text/html' }); resp.write(`<p>Hello, world! You requested ${req.url}</p>`); resp.end(); }).listen(3000);
Start the app. Note you won’t see any output and this is a blocking call.
Then open the browser at:
Note that you can use any path and you’ll get the same response.
Parse the query string
Based on Node.js HTTP Module on W3 Schools.
Use the url
builtin module.
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
var q = url.parse(req.url, true).query;
var text = q.year + " " + q.month;
res.end(text);
}).listen(3000);
Open in the browser:
Result:
2017 July
3rd-party packages
See the Express static server page for using Express.