We will cover the HTTP server module. Technically, you could write your HTTP server using the net
module, but you do not have to.
Some of these functions are very similar to the net
module functions. This should make sense as HTTP, at its core, is a network server.
All of these functions and objects are also used with the HTTPS module. The only difference is that for the options of createServer
and https.request
, you can pass certificates.
All of the following examples assume that the module has been loaded:
var http = require('http');
http.createServer([requestListener])
Much like net.createServer
, this is required to serve anything. The requestListener
parameter is attached to the request event.
Here is a simple example that just logs to the console any time a request is made:
var server = http.createServer(function (req, res) { console.log('Someone made a request!'); res.end...