Skip to main content


Showing posts from January 12, 2018

Introduction to Node

Node.js Node.js is an open-source, cross-platform, JavaScript runtime built on Chrome's V8 Javascript engine that allows developers to create all kinds of server-side tools and applications in JavaScript. Node.js was developed by Ryan Dahl in 2009.
Javascript everywhereGreat performanceAsynchronous and event drivenSingle threadedNode package manager (npm) provides access to thousands of reusable packages.Portable Express Express is an un-opinionated, minimalist web framework for Node.js
Setting up the Node environmentInstall Node $ curl -sL | sudo -E bash -
$ sudo apt-get install -y nodejs
$ node -vInstall express generator The express application generator tool generates an Express application skeleton.
$ npm install express-generator -g
(-g flag installs the tool globally)
Create skeleton project $ express --ejs --git hellonode
$ cd hellonode
$ npm install
$  DEBUG=hellonode:* npm start
then go to br…

Layouts in express ejs


This is the 5th part of our Node EMS.

In my last post I used the same html layout for all the views. But it is still repeating. So one solution for it to use a layout engine. For express ejs we can use express-ejs-layouts.

$ git clone
$ cd node-ems
$ npm install
$ npm run devstart

If you like to check the changes done in this tutorial
$ git checkout layout-with-ejs
Installation $ npm install express-ejs-layouts --save
Usage in app.js we add following,
var expressLayouts = require('express-ejs-layouts');
app.set('layout', 'layout/main');

Then we create main layout file and use <%-body%> to generate final html. All other html files will have only the body content.
out final main.ejs

Creating views with EJS


This is the 4th part of Node EMS.

Express uses Jade/ Pug as default view template engine. But it is not pure html which is difficult for a non-ui guy.
We can change the view engine to EJS using --ejs option when creating the project.

Complete code for this tutorial,
$ git clone
$ cd node-ems
$ git checkout create-views
EJS EJS is a simple templating engine that generate HTML with plain JavaScript. There is no special thing about that since it uses plain HTML and uses JavaScript like syntax to generate dynamic contents.
Supports includes, filtters, etc
Tags <% 'Scriptlet' tag, for control-flow, no output
<%= Outputs the value into the template (HTML escaped)
<%- Outputs the unescaped value into the template
<%# Comment tag, no execution, no output
<%% Outputs a literal '<%'
%> Plain ending tag
-%> Trim-mode ('newline slurp') tag, trims following newline
Create views We are going to use a …

Creating a controller in Node


In this 3rd part of the tutorial, we are going to connect our employee schema with the controller.

(this is incomplete without views on next tutorial)

We are going to use mongoose's built in methods to access database and do our operations.

Update employee controller as follow.

Creating models with mongoose


This is the 2nd part of our Node employee management system. In this part, we are going to create our model.
MongoDB MongoDB is an open source NoSQL database that stores data as documents in JSON like structure. A "collection" of "documents", in a MongoDB database, is analogous to a "table" of "rows" in a relational database.

This combination is extremely popular because it helps to use same structure in entire application. (thus creating stacks like MEAN)
Mongoose Mongoose is an Object Document Mapper(ODM) for Node. (like Hibernate for Java). It allows us to access MongoDB commands simply and easily.
Initialize database * You need to have installed Mongo in your computer.
go to the terminal and do this,
$ mongo
> use nodeems;
> db.employees.insert({
   firstName: 'Manjula',
   lastName: 'Jayawardana',
   email: '',
   password: 'manju'

Node employee management system


Let's do some practical thing using Node.

In this Node tutorial series we are going to build an Employee Management System.
Node introductionNode Employee Management System (EMS) setupCreating models with mongooseCreating a controller in NodeCreating views with EJSCreating layouts get the complete code
$ git clone
$ cd node-ems
$ npm run devstart
navigate to http://localhost:3000

Generate the project $ express --ejs --git node-ems
$ cd node-ems
$ npm install
$ npm install nodemon --save
   update scripts: "devstart": "node ./bin/www"
$ npm run devstart

quickly check the setup: http://localhost:3000/

Let's modify the folder structure a bit. $ mkdir server
$ mv routes/ server/
$ mv views/ server/
$ mkdir server/controllers
$ mkdir server/models

> you need to modify the app.js to pick routes, views from server folder now.

Create new employee-controller in server/controllers
Create employee routes $ mv server/…