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 web development, 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 recommend 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, as you can see in the picture below.
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
Now, we need somewhere to work on the project. So before anything else, let's create a new folder. Inside 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 our names the same.
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 on that module 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');
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 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 it's 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
. It will write out the data you provide, and we've gone ahead and 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 now 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.
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 recommend 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)