Introduction
Node.js allows you to run JavaScript code in the backend, outside a browser. In order to execute it, it needs to be interpreted first. Node.js uses Google's V8 VM, the runtime environment for JavaScript that Google Chrome uses to interpret and execute the code.
Node.js is a command line tool. You download, compile and install the source.
Node.js let's you run JavaScript programs by typing 'node my_app.js' in terminal.
The javascript is executed by the V8 javascript engine.
Node.js provides a JavaScript API to access the network and file system.
Node.js is good when you need to do several things at the same time.
Installing
In order to run Node.js you need the following:- GNU make 3.81 or newer. Pre-installed on most systems. Sometimes called gmake.
- python 2.6 or 2.7. The build tools distributed with Node run on python.
- libssl-dev (Node v0.6.x only.) Can usually be installed on *NIX systems with your favorite package manager. Pre-installed on OS X.
- libexecinfo (FreeBSD and OpenBSD only.) Required by V8. pkg_add -r libexecinfo installs it.
On a Mac
- Install Command Line Tools
Xcode: Preferences>Downloads install Command Line Tools -
Download node.js src code
git clone https://github.com/joyent/node.git cd node git checkout v0.8.2
-
Compile Source Code
export CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang export CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ ./configure && make && sudo make install
router in order to map requests to request handlers request handlers request data handling view logic upload handling implement the first part of our stack, the HTTP server. create a main file Create the file server.js in the root directory of your project,
//requires the http var http = require("http"); //This function returns an object, and this object has a //method named listen, and takes a numeric value which indicates the port number our HTTP server is going to listen on. //assign function to a variable, which you pass, or define the function to pass. http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888);
When the callback function the onRequest() gets triggered, two parameters are passed into it: request and response. request and response are objects, and their methods can used to handle the details of the HTTP request that occur. When a request is received, it uses the response.writeHead() to send the HTTP status 200 and content-type in the HTTP response header, and the response.write() function to send the string “Hello World” in the HTTP response body. response.end() is called to finish the response
index.js which is used to bootstrap and start our application by making use of the other modules of the application
Making some code a module means we need to export those parts of its functionality that we want to provide to scripts that require our module.
var http = require("http"); function start() { function onRequest(request, response) { console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } exports.start = start;
var server = require("./server"); server.start();
Making different HTTP requests point at different parts of our code is called “routing” - well, then, let’s create a module called router.
The url module provides methods which allow us to extract the different parts of a URL (like e.g. the requested path and query string), and querystring can in turn be used to parse the query string for request parameters: server.js
var http = require("http"); var url = require("url"); function start() { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } exports.start = start;
function route(pathname) { console.log("About to route a request for " + pathname); } exports.route = route;
var server = require("./server"); var router = require("./router"); server.start(router.route)
node index.js
http://localhost:8888