In this tutorial I am going to guide you around in working with Node.js!
- Get started with node.js
- Create a hello world app
- Use modules
Get started with node.js
Note: This tutorial is assuming you use Windows as your operating system.
Go download the latest Windows Installer. Install it.
You can go the node console by starting cmd, and entering node. Test if it works by typing: “console.log(‘Hello world’);
You can get out of it by typing Ctrl + C twice.
Let’s try to make our first ‘Hello World’ application!
- Choose a location on your hd where you want to develop your websites.
- Choose an IDE (Integrated Development Environment), you could just use a text writer, or any program you like. I am going to use Eclipse EE, since I am used to working with it.
- Create a file ‘hello_http.js’
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(8080);
Now in cmd go to the folder where you saved the file, start node (by typing: node hello_http.js).
If you now go to your browser (http://localhost:8080), you should see:
What does this very short script do?
var server = http.createServer(function(req, res) { res.writeHead(200); res.end(‘Hello Http’); });Resources
- http://nodejs.org/
- http://nodeguide.com/beginner.html
var http = require('http');
|
Include a node.js core module and make it accessible through the variable http. |
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
|
Create the server. This method gets executed everytime a request comes in.
The “req”, is the request, this can contain form data, or files (upload).
The “res” object is a writeable stream to send data back to the client. |
server.listen(8080);
|
This makes our server listen on port 8080. |
Organize your app with modules
Node.js uses modules to organize your sites. Just like how you can include core modules, you can create your own modules. Below is an example that creates a ‘Hello’ module, used to return the famous ‘Hello World’.
Hello.js:
exports.world = function() {
return('Hello World');
}
The exports.* is the object used to return methods to the scripts that use this module.
Then create another file, Main.js. This will be our start point. The difference is now, that it will use our Hello.js module, to return the ‘Hello World’ string.
Main.js:
var http = require('http');
var hello = require('./hello');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end(hello.world());
});
server.listen(8080);
As you can see we can include hello, with a ‘./’ before (means that the file is in the same folder), and without the extension, because node automatically assumes it is a .js file.
There is more!
This should cover the basics. If you want to do more, try to understand how EventEmitters work in node.js!
I, am currently reading and playing around with expressjs!
Resources
- http://nodejs.org/
- http://nodeguide.com/beginner.html
- http://expressjs.com/