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