What is Node.js?

Where to use Node.js

I/O Bound Applications

JSON API Based Applications

Single Page Applications

Installing Node.js

From Binaries: Download the Node.js Binaries from Official Download Page Go To Downloads page

On Ubuntu:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs

Creating a Simple Node.js Application

Initializing a Node.js Application

mkdir SampleNodeJSApp cd SampleNodeJSApp/

npm init

name : Name of your project in lowercase

version : Version of your project.

description : Description about your Project

entry point : Defines entry point for App. Usually index.js or server.js

test command : Command to execute test suite

git repository : Link of Git repo

keywords : Keywords which describes your application best.

author : Your Name

license : License

Sample Directory Structure for Node.js App

assets : Contains all client-side assets that require compilation css : Contains all your LESS/Stylus style-sheets js : Contains your client-side CoffeeScript/ES6 files

dist : Contains all compiled server-side scripts

LICENSE : License file

package.json : file contains meta data about your app or module

public : Contains your static files that are not handled by any compilers

README.md : Read Me file

routes.js : Contains all routes

server.js : Entry Point

src : Contains all server-side scripts controller : Contains all Controllers middleware : Contains all middleware scripts model : Contains Models if Mongoose is used

test : Contains all unit testing scripts ( implemented using a testing-framework of your choice )

views : Contains all your express views ( pug,handlebars, ejs or any other templating engine )

Some Frameworks Available for Node.js

Express.js : a minimalist framework for building a host of web and mobile applications as well as application programming interfaces (APIs). Hapi.js : a powerful Node.js web framework for building application program interfaces (APIs) and other software applications. Socket.io : a Node.js server framework for building real-time web applications Meteor : an open-source, model-view controller (MVC) framework for building websites and web/mobile applications. Sails.js : Sails.js is one of the most popular real-time frameworks around for building Node.js applications. Sails.js offers a model-view controller (MVC) pattern for implementing data-driven application programming interfaces (APIs).

Some of the Databases that can be used as our app's backend

MongoDB : a free and open-source cross-platform document-oriented database program MYSQL : an open-source relational database management system (RDBMS). PostgreSQL : an object-relational database (ORDBMS)

npm install express --save

npm install mongodb --save

"passport": "~0.2.0", "passport-local": "~1.0.0"

"dependencies": { "bcrypt-nodejs": "latest", "body-parser": "^1.12.4", "connect-flash": "^0.1.1", "cookie-parser": "^1.3.5", "cookie-session": "^1.1.0", "crypto": "^0.0.3", "express": "^4.12.4", "express-handlebars": "^3.0.0", "express-validator": "^2.10.0", "mongodb": "^2.2.22", "mongoose": "^4.8.1", "morgan": "^1.7.0", "passport": "^0.2.2", "passport-local": "^1.0.0", "serve-static": "^1.9.3" }

Mongoose : Mongoose provides a straight-forward, schema-based solution to model your application data.

Morgan : HTTP request logger middleware for node.js

Fork this Repo Fork Clone Forked Repo to your local machine git clone https://github.com/YOURUSERNAME/Sample-Node.js-Application.git Install dependencies npm install Start Application npm start

Now you can build your app on top of this sample application

Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine).Make a New Folder for your Node.js Application and cd into that new folder using Terminal (CMD)Initialize a Node.js Application using NPM package managerNow you will be asked a series of questions. You need to answer according to your project.Now you will be presented a generated package.json file to review.There are hundreds of NoSQL Databases available that can be integrated with Node.js. Finding one for your application type is your responsibility :p.To Proceed with the sample app I'll choose Express.js and MongoDBInstalling Express.jsAssuming MongoDB installed on your systemIf not follow this link Installing MongoDB driverWe will be creating a simple user authentication system using passport.jsAdd passport and passport-local dependencies to package.jsonWe need some other dependencies too.Finally your dependencies should look like thisNow we need to choose a templating engine for our Node.js appPersonally I would go for Handlebars. Because it is similar to HTML. It is upto you to choose a Templating engine. I recommend you to go through each of these templating engines and choose one that suits you.First we need to define a User model for our application using Mongoose. ( or not use Mongoose at all. Read more about this issue I have implemented only local registration part. you can refer passport.js website to know more about social media logins.Now we can implement passport.js login and signup strategiesRoutes will look like thisOur Application's Entry Point Server.js will look like thisYou can find the source to this Sample Application Here