Demo entry 5854579

App.js

   

Submitted by de Rozic on Jul 20, 2016 at 22:29
Language: JavaScript. Code size: 2.7 kB.

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');
var $ = require('jquery');
var http = require('http');
var app = express();

//Setup the router
var router = express.Router();
// route middleware that will happen on every request
router.use(function(req, res, next) {

    // log each request to the console
    console.log(req.method, req.url);

    // continue doing what we were doing and go to the route
    next();
});

// home page route (http://localhost:8080)
router.get('/', function(req, res) {
    res.render('index');
});
router.get('/about', function(req, res) {
    res.render('/about');
});
// apply the routes to our application
app.use('/', router);

app.listen(3000,function(){
  console.log("Bring it at Port 1717");
});
livereload = require('livereload');
server = livereload.createServer();
server.watch([__dirname + "/assets", __dirname + "/views"]);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//setup for partials

// setup the ability to have development partials and local environments to support coding

var env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';

app.use(favicon(path.join(__dirname, 'assets/images', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
// add the assets directory with static resources for js img and semantic
app.use(express.static(path.join(__dirname, 'assets')));

// setup routing - old way prerouter functions
//app.use('/', index);
//app.use('/about', about);
//app.use('/users', users);

// 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 handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).