Wednesday, February 1, 2017

Installation and validation for Express


Installation


1.we can Install express globally or locally
$ npm install express -g

2 .Create package.json file


$ npm init

  Response from the bash command
Press ^C at any time to quit.
name: (bookstore)
version: (1.0.0)
description: Simple book
entry point: (app.js)
test command:
git repository:
keywords:
author: Prathap Kudupu
license: (ISC)
About to write to C:\Development\NodeProjects\bookstore\package.json:

{
  "name": "bookstore",
  "version": "1.0.0",
  "description": "Simple book",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Prathap Kudupu",
  "license": "ISC"
}


Is this ok? (yes)

Package.Json would be automatically created


3.Add dependencies in the package.json file
  1. Express : Web framework for node.js 
  2. Body-parser (Interceptor): This would make sure that we would be able to submit forms and make post request and get the values from the form
         Parse incoming request bodies in a middleware before your handlers, available under                            the req.body property.
  1. Mongoose: MongoDB Object modeling tool designed to work in asynchronous environment
{
  "name": "bookstore",
  "version": "1.0.0",
  "description": "Simple book",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "express":"*",
    "body-parser":"*",
    "mongoose":"*"
  },
  "author": "Prathap Kudupu",
  "license": "ISC"
}


Note:
To install these dependencies we would use npm install .
If  default installation fails then we need individually install express and other dependencies

c:\Development\NodeProjects\bookstore>npm install body-parser

ap.js file
c:\Development\NodeProjects\bookstore>npm install body-parser//web framework for node.js
var express= require('express')
var app=express();
//Used for post request and forms
var bodyParser=require('body-parser');
//MongoDB Object modeling tool designed to work in asynchronous environment
//var mangoose=require('mongoose');

//connect to mongoose
//mongoose.connnect('mongodb://localhost/boostore');
//database Object
//var db= mongoose.connection;
//handle route note:/ represents the home page
//The below function takes request and response
//Next function for chaining
app.get('/',function(req,res,next){ //sends the response to the browser res.send("Hello World"); }); //Specify a port to listen app.listen(3000); console.log('Running on port 3000')

Validation

Command to run the app
c:\Development\NodeProjects\bookstore>node app
Running on port 3000

Output

No comments:

Post a Comment