Full series: Building website with Nodejs - A post from my experience

Install Nodejs, npm using nvm

There are several ways of installing Nodejs. You can install it using your OS packages manager or compiling it from source. However, the recommended way is to install it using nvm. nvm can help you install, maintain and run multiple versions of Nodejs (without sudo need).

To install nvm, simply do

$ git clone https://github.com/creationix/nvm.git ~/.nvm

Add this to your shell’s rc file

$ source ~/.nvm/nvm.sh

Next, install one version of nodejs that you want using nvm and set it as the default

$ nvm install 0.10
$ nvm alias default 0.10

Installing nodejs with nvm will automatically install npm (packages manager for Node). For more information about nvm, visit it’s homepage at https://github.com/creationix/nvm.

To test whether nodejs works correctly, you can use the example from nodejs page. Create a file named example.js

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 run it using nodejs

$ node example.js
Server running at http://127.0.0.1:1337/

Auto reload app with nodemon

Usually, every time you change your code, you have to manually restart node server in order for the changes to take effect. Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Just use nodemon instead of node to run your code. To install it

$ npm install -g nodemon
$ nodemon example.js

Install Express and generate the website structure

Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications. You can install Express with npm

$ npm install -g express-generator

Next, use the newly installed express to generate basic site structure. Replace myapp in the command below with your desired app name

$ express --sessions --css stylus --ejs myapp
$ cd myapp && npm install

The first command will generate a skeleton for your web app with session, stylus css and ejs render engine support. The second one will install all the dependencies that you app needs. To run the website, execute app.js using nodejs or nodemon

$ nodemon app.js
19 Mar 13:56:01 - [nodemon] v1.0.15
19 Mar 13:56:01 - [nodemon] to restart at any time, enter `rs`
19 Mar 13:56:01 - [nodemon] watching: *.*
19 Mar 13:56:01 - [nodemon] starting `node app.js`
Express server listening on port 3000

Open your web browser and go to http://localhost:3000. You should see something like this

Alt Text

Next: Nodejs - Express with ejs/stylus basics