Creating our First Server

Open up your server.js file if you're following along.

Our end product will be as follows:

// server.js
// import the http module
var http = require('http');

// handle sending requests and returning responses
function handleRequests(req, res) {
  // return string
  res.end('Hello world!');
}

// create the server
var server = http.createServer(handleRequests);

// start server and listen on port x
server.listen(8080, function() {
  console.log('Listening on port 8080');
});

Then run node server.js and navigate to localhost:8080 and you should be up and running with your first Node site!

Table of Contents

    Chris Sevilleja

    173 posts

    Co-founder of Scotch.io. Slapping the keyboard until something good happens.