How to use NodeJS to implement a webserver in Ubuntu?

If your Ubuntu is completely freshly installed, then it is necessary to install a lot of development-related accessories:

Alternatively the fastest way to install NodeJS, is to get from the NodeJS development site:

git clone git://
cd node
sudo make install

And "node" will be installed into the /usr/local/bin directory by default.

In Ubuntu, as the following article described:

You can also use "apt-get install nodejs" to install NodeJS – but it is not the latest version. Eg, for Ubuntu 32-bit 12.04.2 LTS, the version seemed to be 0.6.12, which is not able to install "express".

/usr/bin/node -v

For /usr/local/bin/node -v:

And note that node is not nodejs:

(but /usr/local/bin/node binary is for NodeJS :-)).

Now install npm’s package called "express" by "npm install express" (npm is part of the NodeJS installation):

(More information is available here:

npm http 200
npm http GET
npm http 200
npm http GET
npm http 200
npm http GET
npm http 200
npm http GET
npm http 200
npm http 200
npm http 200
npm http 200
npm http 200
npm http 200
npm http 200
express@3.3.4 node_modules/express
├── methods@0.0.1
├── fresh@0.1.0
├── cookie-signature@1.0.1
├── cookie@0.1.0
├── debug@0.7.2
├── buffer-crc32@0.2.1
├── range-parser@0.0.4
├── mkdirp@0.3.5
├── send@0.1.3 (mime@1.2.9)
├── commander@1.2.0 (keypress@0.1.0)
└── connect@2.8.4 (uid2@0.0.2, pause@0.0.1, qs@0.6.5, bytes@0.2.0, formidable@1.0.14)

Next is to write the following script and named it as "server.js":

var fs = require(“fs”);
var host = “”;
var port = 1337;
var express = require(“express”);

var app = express();
app.use(app.router); //use both root and other routes below
app.use(express.static(__dirname + “/public”)); //use static files in ROOT/public folder

app.get(“/”, function(request, response){ //root dir

app.listen(port, host);

Or another alternative piece of code:

var http = require(‘http’);
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World\n’);
}).listen(1337, ‘’);
console.log(‘Server running at’);

And then followed by "node server.js" to execute the javascript and a port at 1337 will be opened up. Use a browser to go to http://localhost:1337/ and you will be welcomed with a "Hello" message. (NOTE: both the piece of javascript code above are definitely not mine originally :-)).


2 responses to this post.

  1. Running the fuzzer “NodeFuzz” (from using “gdb node” and then “run nodefuzz.js” (as downloaded from NodeFuzz) I got some stack trace (other details are available via

    #0 0x08566ad0 in v8::internal::Parser::Parse() ()
    #1 0x083b7f8e in v8::internal::MakeFunctionInfo(v8::internal::CompilationInfo*) [clone .constprop.259] ()
    #2 0x083b8a4e in v8::internal::Compiler::Compile(v8::internal::Handle, v8::internal::Handle, int, int, v8::internal::Handle, v8::Extension*, v8::internal::ScriptDataImpl*, v8::internal::Handle, v8::internal::NativesFlag) ()
    #3 0x0839af65 in v8::internal::Genesis::CompileScriptCached(v8::internal::Isolate*, v8::internal::Vector, v8::internal::Handle, v8::internal::SourceCodeCache*, v8::Extension*, v8::internal::Handle, bool) ()
    #4 0x0839b15e in v8::internal::Genesis::CompileNative(v8::internal::Isolate*, v8::internal::Vector, v8::internal::Handle) ()
    #5 0x0839d154 in v8::internal::Genesis::InstallExperimentalNatives() ()
    #6 0x083a2a21 in v8::internal::Bootstrapper::CreateEnvironment(v8::internal::Handle, v8::Handle, v8::ExtensionConfiguration*) ()
    #7 0x08387cc1 in v8::Context::New(v8::Isolate*, v8::ExtensionConfiguration*, v8::Handle, v8::Handle) ()
    #8 0x081e1101 in node::Start(int, char**) ()
    #9 0x081cdf3b in main ()

    Googling for some of the symbols above easily lead to more fuller explanation of the Node internal architecture (eg, how V8 javascript engine API is used as its internal parser). For more info:


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: