Create a 'Hello Word' Server with Node.js
Tags: javascript, node, codenewbie, 100daysofcode
Published: 2021-09-15 19:52:05

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](https://nodejs.org/en/). 

I highly reccommend you watch the [tutorial on CodeCast](https://www.codecast.io/casts/2aX5Zo1aplJ3-create-a-basic-node-js-server) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6r4spfc1fuse7mu2ret.png)

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](https://nodejs.org/api/http.html).

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

     const http = require('http');

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

     const hostname = '127.0.0.1';
     const port = 8000;

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) => {
     });

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](https://en.wikipedia.org/wiki/List_of_HTTP_status_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' 
       });
     });

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');
     });

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}/`);
     });

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhx1ajsyzxubzzzzgu31.png)

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](https://play.codecast.io/2aX5Zo1aplJ3-create-a-basic-node-js-server) 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.