I remember trying to get into starting with Node.js months ago and, to be frank, it wasn’t the simplest process. Installation was a hassle, and pretty much required the use of a Linux of Mac computer. Now, Node.js has evolved to the point where just about anyone can get started using Node.js (maybe not my grandparents, but they don’t even have a computer). Let’s get started!
Seriously though, Node.js has come a long way in the past few months, especially in terms of getting it up and running on your computer. Now, you can just hop on over to http://nodejs.org/#download and click the link for the installer that you wish to use. If you’re using 64-bit Windows, make sure you notice the link lower down labeled “Windows x64 Installer”. After it’s downloaded, just run it, follow the prompts, and Node will be installed and working on your computer. To be sure that it’s working open up a terminal window (in Linux or Mac, start up the Terminal, and on Windows use Windows Powershell – if you have it – or a normal command prompt). Inside here just type in “npm”. It should give you instructions on how NPM should be used. Then just type in “node”. It should just give you an angle bracket pointing to the right. This means that you’re inside the Node REPL, which means Node is working.
- The environment: you are not in the browser. You do not have access to any sort of DOM. You don’t even have the
windowglobal. Instead there are several globals and all other variables are localized to files/modules.
- There are built in commands for the REPL that allow you to use it more like a standard command prompt. If you type in
.help, which is one of the commands, you’ll see a list of commands that can be used.
Here’s the point where you stop reading and go play around for a couple hours and then come back. =)
Save it and then jump back to the terminal (if you even left it). Make sure you’re inside the directory where you created the file. Now type in
node app.js (make sure to replace app.js with the name of the file you created if you were cool enough to use an alternate file name). Great! Now your tiny little server is running. Navigate your browser on over to http://127.0.0.1:1337/ to see your message!
Now let’s walk through the code here. The first line calls
require, which is a global function that just loads a file named http.js. If this file isn’t found in the current directory, it’ll pull it from the standard libraries that come with Node.js. When you
createServer takes a callback function that is executed every time the server receives a request and it sends objects representing the request and response into that callback function.
createServer also returns a server object. Now let’s take a look at the contents of the callback. Notice, we never touch the request object. With a good server used for real applications, we would take a look at the request and pull information like the path and query parameters to determine what should be returned, but here we don’t care. In fact you could navigate your browser to http://127.0.0.1:1337/any/path/you/want?and=any¶ms and it would still show the “Hello World” message.
We do work with
res though. First we call
writeHead on it, which sets headers for the response. Here we’re sending a 200 status and setting the content type to plain text. Next we call
end, which means we’ve reached the end of the response so nothing more can be added to it and it should be shipped out to the requester. We could have also written that line like this:
In other words, calling
end is just like calling
write and then ending the response. The
write method just writes more data to the response. Now, we’re done with the callback, so we call
listen on the server.
listen takes a port number and an IP address to listen to for requests. When it receives a request there, it calls the callback that we just finished creating. By using
node app.js that it didn’t go back to the normal command prompt line after it finished executing because the process is still running and waiting for requests. If you want to stop the server hit Ctrl/Cmd + C, which will break the execution of the process.
The last thing we do in app.js is a simple
console.log, which signals use that the server is indeed running. If we didn’t do this we’d just see a blank line in the console until we terminated the process.
This may be the end of this tutorial, but there’s still tons more to learn about Node.js. Previously you saw Mike M. Lin’s tutorial on getting started with Express, which is a nice framework for simplifying the setup and development of Node-based sites, but I’m not so sure that it’s my cup of tea. I still have a few more things related to Node to talk about before I get into any higher level development, but eventually I should be looking at at least one framework. I hope you’re all looking forward to more. God Bless and Happy Coding!