Setting up a router
In order to deliver web content we need to make a URI available. This recipe walks us through the creation of an HTTP server that exposes routes to the user.
Getting ready
First, let's create our server file. If our main purpose is to expose server functionality, it's general practice to call the file server.js
, which we could put in a new folder. It's also a good idea to install and use hotnode:
sudo npm -g install hotnode
hotnode server.js
Hotnode
will conveniently auto-restart the server when we save changes.
How to do it...
In order to create the server we need the http
module, so let's load it and use the http.createServer
method:
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, ...
Get Node Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.