How to Learn NodeJS as a Django Developer?


Part - 1

  1. Installation
  2. Project Creation
  3. App Structuring
  4. Setup AngularJS

Part -2

  1. Integrate Database
  2. Setup Login
  3. Setup Worker(node celery with Rabbitmq)
  4. Deploy

1. Installation

Install node

Install npm

Install express
npm install -g express

2. Project Creation

Creating using express-generator

express <myapp>
cd <myapp>
npm install

To Start: npm start

3. App Structuring

Rename app.js as manage.js (As you like)

In your project find www file under bin folder and change

var app = require('../app');


var app = require('../manage');

Configure templates folder

In your manage.js configure app.set('views', path.join(__dirname, 'views'));

views is the directory from where your templates are going to rendered

So you can change this name and make sure this directory exists in your project

app.set('views', path.join(__dirname, 'templates')); (As you like)

Configure templating engine

NodeJS supports different template engines

hjs (hogan js)
ejs (embererd js)

Note: All these templates are rendered in server side by node

In your manage.js configure the template engine app.set('view engine', 'jade');

In above it uses the jade as template engine

app.set('view engine', 'hjs'); (As you like)

npm install hjs or npm install jade

Configure Static folder

By default NodeJS will look into Public for static content, If you
want to use someother folder specify the folder name in the following

app.use(express.static(path.join(__dirname, 'public')));


app.use(express.static(path.join(__dirname, 'assets')));

The assets folder should exists in your project.

Creating Apps

In NodeJS unlike django there is no concpet of apps, everything grouped under what they do. Ex: urls are under routes, templates under views.

In your project folder

mkdir <app1>
mkdir <app2>


urls.js (empty)
models.js (empty)
views.js (empty)

in your app1 and app2 (As you like)

Create folder for your settings and other utils with same as your project name (As you like)

in your project,

mkdir <yourprojectname>

in yourproject/yourprojectname/

create a file called settings.js (As you like)

Some tweeks to settings

Like django if we want to use some settings or some automated loading based on settings then first we need to maintain some settings

var CONFIG = {
module.exports.config = CONFIG;

In the above code I'm maintaining installed apps to load urls automatically in manage.js with list of INSTALLED_APPS

Understanding module.exports

At this point of time you may be suprised or misconfused about module.exports and what it does.

It will export the export the specified thing into where you require it.

Autoload urls

Means it will automatically import urls like django,

Import settings in your manage.js

var settings = require('.//settings');
load urls.js automatically
 for(var iter = 0; iter < settings.config.INSTALLED_APPS.length; iter++){
 var route = "./" +settings.config.INSTALLED_APPS[iter] + "/urls";
 var routes = require(route);

4. Setup AngularJS

Note: Use only the non-minified version of the script libraries of angularjs.

include js files assets (assets/js)

add script tags in base.hjs

Re-Route to client when partial url comes to nodejs

if(err.status === undefined || err.status === null){
 res.render('base', {'message':'partial_url'});
 } else {
 res.status(err.status || 500);
 res.render('error', {
 message: err.message,
 error: {}

I'll post the part-2 with some basic actions like login setup, database setup and deployment of node app in ec2 very soon.