A Node HTTP Server

| Comments

Just like c# (which is my first language), you can import libraries with Node. They’re called modules. One of these modules is ‘http’. Let’s see how we can create a http server with this module.

hellopizza.js
1
2
3
4
5
6
7
8
9
10
11
12
var http = require('http');

//the http module has a function: createServer which takes a callback as a parameter:

http.createServer(function(request,response) {
       response.writeHead('200');
       response.write("one pepperoni pizza please");
       response.end();

    }).listen(8080);

console.log("listening on 8080");

We can run this with node:

1
2
jacqueline:public$ node hellopizza.js
listening on 8080

Browse to the page with curl:

curl
1
2
jacqueline:$ curl localhost:8080
one pepperoni pizza please

Comments