text:  var express = require('express');
  var path = require('path');
  var mongoose = require('mongoose');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieSession = require('cookie-session');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var app = express();
  
  //reqire routes folder
  var index = require('./routes/index');
  
  mongoose.connect('mongodb://localhost/notesapp',{ useNewUrlParser: true });
  
  var db = mongoose.connection;
  
  //handle mongodb error
  db.on('error', console.error.bind(console, 'connection error:'));
  
  db.once('open', function () {
  
    console.log("Connected to DataBase...");
  });
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  
  app.set('trust proxy', 1) // trust first proxy
  
  app.use(cookieSession({
    name: 'session',
    keys: ['abc', 'xyz']
  }))
  
  app.use(function (req, res, next) {
  
      // Website you wish to allow to connect
      res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
  
      // Request methods you wish to allow
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  
      // Request headers you wish to allow
      res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  
      // Set to true if you need the website to include cookies in the requests sent
      // to the API (e.g. in case you use sessions)
      res.setHeader('Access-Control-Allow-Credentials', true);
  
      // Pass to next layer of middleware
      next();
  });
  
  app.use('/', index);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    //res.render('error');
  });
  
  module.exports = app;
  