Sunday, August 2, 2015

sample routing programs with Expressjs in Node


There are some difference between express3.X and 4X in usage of app.router and bodyParser

how to avoid below errors in nodejs and expressjs

Most middleware like bodyparser is no longer bundled with Express

means you have not installed bodyparser , you have to install it as below
npm install body-parser

use this site for usage for body-Parser

https://www.npmjs.com/package/body-parser


Difference between express3.X and 4.X

https://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0



App.js

ignore the commented lines in the below code

var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
var bodyParser = require('body-parser');
 /*var urlencodedParser = bodyParser.urlencoded({ extended: false });*/
 app.use(bodyParser.urlencoded());
 app.set('port',process.env.PORT || 3000);
 app.set('views',__dirname + '/views');
 app.set('view engine','jade');
 /*app.use(app.router);*/
 /*var people = express.Router();*/
app.use(express.static(path.join(__dirname,'public')));

/*app.get('/about',function(req,res){
  res.send("about naveen");
 });*/
 
app.post('/user',function(req,res){
  res.send("requested user name" + req.body.name);
});
 
/*app.get('/user/:username',function(req,res){
  res.send(" "+ req.params.username + " profile");
 });*/
 /*app.get('/',function(req,res){
  res.send("hello......");
 });*/
 //app.use(express.static(path.join(__dirname, 'public')));
 var server = http.createServer(app).listen(app.get('port'),function(){
      console.log('Express server listening on port' + app.get('port'));
 });

place the file index.html in the Public folder


then start node app.js



No comments: