What is Node.js?
Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine).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
Make a New Folder for your Node.js Application and cd into that new folder using Terminal (CMD)mkdir SampleNodeJSApp
cd SampleNodeJSApp/
Initialize a Node.js Application using NPM package managernpm init
Now 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.
- 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)
To Proceed with the sample app I'll choose Express.js and MongoDB
Installing Express.js
npm install express --save
--save option will add express to package.jsonAssuming MongoDB installed on your system
If not follow this link
Installing MongoDB driver
npm install mongodb --save
We will be creating a simple user authentication system using passport.jsAdd passport and passport-local dependencies to package.json
"passport": "~0.2.0",
"passport-local": "~1.0.0"
We need some other dependencies too.Finally your dependencies should look like this "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"
}
Now we need to choose a templating engine for our Node.js appFew Popular Choices will be
Personally 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.
Or you can use AngularJS , ReactJS or Something like VueJS for the frontend
Some Additional Modules used in Sample Application
- Mongoose : Mongoose provides a straight-forward, schema-based solution to model your application data.
- Morgan : HTTP request logger middleware for node.js
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 strategies
Routes will look like this
Our Application's Entry Point Server.js will look like this
Final Output
Final Folder Structure
You can find the source to this Sample Application Here
GITHUB REPOSITORY
Guide to setup
- Fork this Repo Fork
- Clone Forked Repo to your local machine
- Install dependencies
- Start Application
git clone https://github.com/YOURUSERNAME/Sample-Node.js-Application.git
npm install
npm start
0 comments:
Post a Comment