Created
April 23, 2017 04:29
-
-
Save neeraj87/1318175ce8699b4d1f72d8eb67fb1158 to your computer and use it in GitHub Desktop.
Node app.js starter template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); //create a new express app | |
var exphbs = require('express-handlebars'); | |
var router = express.Router(); //use express 4.0 router to define routes | |
var bodyParser = require('body-parser'); //pull information from html post | |
var cookieParser = require('cookie-parser'); | |
var http = require('http'); | |
var path = require('path'); | |
var winston = require('winston'); | |
var app = express(); | |
app.engine('handlebars', exphbs({defaultLayout: 'main'})); //set the app engine and default layout name 'main' | |
app.set('port', process.env.PORT || 8000); //set port to either environment port OR 8080 | |
app.set('views', path.join(__dirname, 'views')); //by default, express expects its template files to be in the views folder. In case you have a different path, you can update it here. | |
app.set('view engine', 'handlebars'); //define the view engine | |
app.use(express.static(path.join(__dirname, 'public'))); //declares the location of static resources (css, js ,images) | |
app.use(bodyParser.urlencoded({extended:true})); // parse application/x-www-form-urlencoded | |
app.use(bodyParser.json()); | |
app.use(cookieParser()); // read cookies (needed for auth) | |
app.use(function(req, res, next) { | |
next(); | |
}); | |
process.on('uncaughtException', function (err) { | |
winston.log('info', '-------------- UNCAUGHT EXCEPTION: ' + err); | |
winston.log('info', '------------- ERROR STACK -------------'); | |
winston.log('info', err.stack); | |
winston.log('info', '---------------------------------------'); | |
}); | |
//create server and listen to the port | |
http.createServer(app).listen(app.get('port'), function(){ | |
winston.log('info', 'The server has started'); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment