DEV Community

Cover image for Create a 'Hello Word' Server with Node.js
Amy Oulton
Amy Oulton

Posted on

Create a 'Hello Word' Server with Node.js

JavaScript is famous for being insanely powerful. Once Node.js came onto the market, it meant JavaScript's abilities became even more powerful.

Often when learning, Node.js is one of the first things you learn after JavaScript, because it lets you use JavaScript outside of the scope of a browser.

Today, we're going to make our first server together using Node.js.

I highly reccommend you watch the tutorial on CodeCast because in it I give you tons of tips that won't be included in this post. You can also copy all the code from there, because it appears in the Player interactively as I write it, like you can see in the picture below.

The CodeCast Player

First things first, we need to make sure we have node installed. Go ahead before starting the tutorial and do that by following the instructions on their website (link above).

Create A Directory

At the beginning of any new project, we need somewhere to work on the project. So before anything else, let's create a new folder. Inside of the folder, we just want a single file called app.js. It doesn't need to be named this, but for the sake of following along, it will be easier if we keep it named this.

Begin Building The Server Structure

One of the first things we will always need to do is to give it access to the http module. You can read more up on module that on the docs.

To give it access we'll go ahead and add the following line to the top of the file:

 const http = require('http');
Enter fullscreen mode Exit fullscreen mode

We also need to give our server the hostname and port.

 const hostname = '127.0.0.1';
 const port = 8000;
Enter fullscreen mode Exit fullscreen mode

The next step is to create our actual server. We do this using the createServer() method on the http module. The createServer() method takes a request and a response, so the shell of it looks something like this:

 const server = http.createServer((request, response) => {
 });
Enter fullscreen mode Exit fullscreen mode

Next, we want to tell the server what to do. In this case, we just want to listen to the response on the createServer() and do something if it's successful. It's worth learning some basic server codes in general, but for the purpose of this tutorial, you need to know that 200 is a successful response.

What we're going to do is now tell the server to do something if it gets that 200 response, and what we're going to ask it to do is to use the writeHead() method on the response to send a header to the request. We'll do that like this:

 const server = http.createServer((request, response) => {
   response.writeHead(200, { 'Content-Type': 'text/plain' 
   });
 });
Enter fullscreen mode Exit fullscreen mode

This is essentially just telling the request that what its sending through to the header is text and it should be rendered out as such.

Lastly, to finish building out the server, we are going to use the .end() method on the response. This is telling the server that all the body and headers have been sent through. We are able to provide it with some parameters, and one of those parameters is called data. What it will do with the data you provide it is to write it out, and we've specified in the previous request that we're writing out the data as 'text'.

So now, your server will look like this:

 const server = http.createServer((request, response) => {
   response.writeHead(200, { 'Content-Type': 'text/plain' 
   });
   response.end('Hello World');
 });
Enter fullscreen mode Exit fullscreen mode

Now, the last step is to make something so that it shows in our terminal that the server is running. To do this, we're going to use the listen method on the server as follows:

 server.listen(port, hostname, () => {
    console.log(`The server is running at 
    http://${hostname}:${port}/`);
 });
Enter fullscreen mode Exit fullscreen mode

Now we can move over to our terminal. Make sure you're inside the correct directory and then type in node app.js.

Note: If you called your file something different, you need to ensure that the app.js is replaced with the correct file name.

Terminal View of Starting the Server

Once you see that message, just copy the url which is set to: http://127.0.0.1:8000 and paste it into the browser. You should see 'Hello World' in the window!

Again, I highly reccommend watching the full tutorial on CodeCast because you'll get a lot more out of it than just reading this post.

Congrats! You've created your first server. From this point, you can do an endless amount of stuff.

Top comments (0)