Node as a Web Server

Unveiling the web serving capabilities of the Node.js platform.

Scroll down...

Content

Resources

Comments

This lesson will explain how to get started with Node.js as a web server. We'll get you up and running by starting up a web server with the HTTP library and show you how to respond with a "Hello world!" for your first client-server connection with Node.

Serving with Node.js

As an asynchronous event-driven JavaScript runtime, Node is designed to build scalable network applications. In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Let's talk about the famous boilerplate code that Node provides on their site and in their API documentation. What is going on here?

  1. We require the HTTP module

    const http = require('http');
    
  2. We set the host and port on which the server will listen for requests

    const hostname = '127.0.0.1';
    const port = 3000;
    
  3. We make a call to create the server passing in a callback to run when a request is received

    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
  4. Finally, we tell the server what port and host to listen on for requests

    • As an added convenience, we output where the server is listening in a message to the console
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    

Essentially, we've just witnessed the world's smallest Node web application. It simply responds with plain text, but all the elements that we've discussed in previous lessons are there. If you were to run this code, you could open up http://localhost:3000 in your browser and see the "Hello world!" message.

That's basically it! As you can see, Node has made setting up a server incredibly easy, so you can focus on the more complicated tasks of building robust web applications. But what about those few lines in the request/response callback?

The assignment of the res.statusCode and call to res.setHeader deal with the HTTP status code and HTTP headers that the browser needs to interpret the success/failure and intent of the response from the server. These are a few examples of the settings and configurations necessary to effectively transfer data using HTTP and we'll be looking at them more closely next.

Wrapping Up

Getting started with a Node server should make you feel excited! You're most likely eager to start serving HTML pages now that you see how easy it is to create a server in Node.js. Next, we'll take a closer look at how to handle receiving requests and respond to them with HTML.



Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Handling HTTP Requests with Node