Like I promised, we’ll be creating a VERY simple web blog. Trying to build a fully built web application in Node.js is pretty difficult. So, we’ll be building a simple web app and then learn how to utilize frameworks to help make the process a whole lot smoother. So bare with me here. If things seem hard, then I promise it’ll become a lot simpler when we start talking about web frameworks.
Node.js offers low level APIs for us to use. This means that handling cookies, sessions HTTP, and rendering HTML is completely up to you. It means a lot more work for you to do, but a whole lot more flexibility than most languages provide. In this tutorial, we’ll learn how to create a HTTP server.
Requiring a Module
Clear out your index.js file that you created in the previous tutorial. Insert the following code below.
var http = require( "http" );
So, what’s going on here? We’re creating a variable called http. We don’t have to call it http, but it’s common practice to name your variables after the modules they’re set to.
What are modules?
We won’t be going too in-depth into what modules are in this tutorial, but in a later tutorial. In it’s most basic form, modules are just JavaScript files outside of the main application. Node.js provides some modules for you to use. You can also create your own and use third party modules.
To use a module, you have to use the require() function.The require() function will search for the module you would like to use. The require() function will only search for JavaScript files. We omitted the .js file extension because this function automatically assumes it’s a JS file. You can include the .js extension if you’d like and it would work just the same. Throughout this tutorial series, we will not be including the .js file extension.
As I stated before, Node.js automatically installs the http module for you. You don’t have to worry about downloading it as it’s made available globally in your system.
What’s happening when you require a module?
Quite a few things and as I’ve stated before, I’ll discuss what modules do in more detail in a future tutorial. For now, all you need to know is that you now have access to properties and methods related to that module.
We can access these properties and methods through out http variable. The first function we’ll be looking at is the createServer() method. Let’s use this method. Update your script to this.
var http = require( "http" ); var server = http.createServer(function( request, response){ console.log("A request has been made!"); response.end( "<h1>Welcome to my blog!</h1>"); }); server.listen( 8080 );
WOAH! A lot is going on here, but let me break it down for you. It’s simpler than you think!
We create a variable called server and set it’s value returned by our http module’s createServer() method. The createServer() method will do as it’s name states. It’ll create a server. We must pass in a function that will handle the requests. The function we define will be passed in 2 objects. The request and response object. These are created for you by the createServer() method. The request object will give you information about the visitor, the page their requesting and more! The response object gives you methods and properties that allow you to handle the request. You could respond with a message or a file. It’s up to you.
Inside our function we write a message into our console telling us that a request has been made. We use the end() method of the response object to write a message to the browser. This is completely optional though. You can pass nothing into the end() method and everything will work just fine.
The problem with using the end() method is that once we call it, we can no longer update the response object. What if we wanted to output more than our welcome message?
Luckily, the response object provides us with another method called the write() method. We can call this method as much as we want without having to worry about the response closing.
It’s important to know that you must call the end() method at some point in your application. Let’s update our code to this.
var http = require( "http" ); var server = http.createServer(function( request, response){ console.log("A request has been made!"); response.write( "<h1>Welcome to my blog!</h1>"); response.end(); }); server.listen( 8080 );
We use our write() method to write our message instead of the end() method. At the end of our script we’re now using the listen() method. We gain access to this method when we use the http’s createSever() method.
The listen() method will simply make the server active. We have to pass in a port number. If you’re not familiar with ports, then you don’t have to worry much about it. You can think of a port as a door to a house. Your computer uses a lot of ports so we have to use a port that’s unavailable. Generally, ports in the thousands are always empty. So we use port 8080 for no particular reason. You could use port 3453 if you wanted to.
We’re done! Let’s execute this script using the command below in the command prompt. Make sure you’re in the same directory as your script or it will not work.
node index
You won’t see anything happen in your console, but don’t worry because our server is up and running! To view your site just go to http://localhost:8080. You should see something similar to the image below.
Wait! Why is our console message being outputted twice even though I only visited my site once? This is because your browser makes an additional request for a favicon. I know it seems strange, but it’s just how your browser works.
You may have also noticed that our console doesn’t show the current directory you’re in anymore. It’s just a little underscore blinking. This is because your application is still listening for requests. The only way to shut down your server is for an error to occur or for you to manually turn it off. You can manually stop it by closing the command prompt or by pressing Ctrl + C on your keyboard.
Conclusion
We learned a little about the http module and learned how to create our own HTTP server. Very simple so far, but right now our application is completely static. Outputting the same message and we can’t even submit a new blog post. In the next few tutorials, we’ll address these problems and create a more dynamic site. If you would like to learn more about Node.js’s http module then click here!