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:

http://davidtsadler.com/archives/2012/05/06/installing-node-js-on-ubuntu/

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

git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

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

In Ubuntu, as the following article described:

http://stackoverflow.com/questions/16302436/install-nodejs-on-ubuntu-12-10

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
v0.6.12

For /usr/local/bin/node -v:
v0.11.5-pre

And note that node is not nodejs:

http://superuser.com/questions/335273/installing-node-js-after-apt-get-install-node

(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: https://npmjs.org/package/express)

...
...
npm http 200 https://registry.npmjs.org/qs/0.6.5
npm http GET https://registry.npmjs.org/qs/-/qs-0.6.5.tgz
npm http 200 https://registry.npmjs.org/bytes/0.2.0
npm http GET https://registry.npmjs.org/bytes/-/bytes-0.2.0.tgz
npm http 200 https://registry.npmjs.org/formidable/1.0.14
npm http GET https://registry.npmjs.org/formidable/-/formidable-1.0.14.tgz
npm http 200 https://registry.npmjs.org/uid2/0.0.2
npm http GET https://registry.npmjs.org/uid2/-/uid2-0.0.2.tgz
npm http 200 https://registry.npmjs.org/mime/-/mime-1.2.9.tgz
npm http 200 https://registry.npmjs.org/pause/-/pause-0.0.1.tgz
npm http 200 https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
npm http 200 https://registry.npmjs.org/qs/-/qs-0.6.5.tgz
npm http 200 https://registry.npmjs.org/bytes/-/bytes-0.2.0.tgz
npm http 200 https://registry.npmjs.org/formidable/-/formidable-1.0.14.tgz
npm http 200 https://registry.npmjs.org/uid2/-/uid2-0.0.2.tgz
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 = “127.0.0.1”;
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
response.send(“Hello!!”);
});

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, ‘127.0.0.1’);
console.log(‘Server running at http://127.0.0.1:1337/’);

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 :-)).

Advertisements

2 responses to this post.

  1. Running the fuzzer “NodeFuzz” (from http://code.google.com/p/ouspg/wiki/NodeFuzz) using “gdb node” and then “run nodefuzz.js” (as downloaded from NodeFuzz) I got some stack trace (other details are available via http://pastebin.com/CG7VRBsU):

    #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:

    http://blog.zenika.com/index.php?post/2011/04/10/NodeJS

    http://www.openwebosproject.org/docs/developer_guide/javascript_services/#.Ue1ZbbQ-dj0

    Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: