How to learn NodeJS as a Django Developer ?

How to learn NodeJS as a Django Developer ?

Are you a python/django developer ? and wants to learn js/nodejs ? then you can startoff with this tutorial. In this part we're going to discuss basic setup, app structuring and also angularJS setup.

Part - 1

1. Installation

2. Project Creation

3. App Structuring

4. Setup AngularJS

Part -2

5. Integrate Database

6. Setup Login

7. Setup Worker(node celery with Rabbitmq)

8. Deploy

1. Installtion

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');

to

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)
jade

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)

Note:
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')));

to

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

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

create,

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 = {
INSTALLED_APPS : [
'app1',
'app2'
],
}
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('./<projectname>/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);
app.use(routes);
}

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.