Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
MusicPlaylist_Dushan/app.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
87 lines (70 sloc)
2.11 KB
This file contains 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'); | |
var path = require('path'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var exphbs = require('express-handlebars'); | |
var expressValidator = require('express-validator'); | |
var flash = require('connect-flash'); | |
var session = require('express-session'); | |
var passport = require('passport'); | |
var LocalStrategy = require('passport-local').Strategy; | |
var mongo = require('mongodb'); | |
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/MusicWebApp', { useNewUrlParser: true }); | |
var db = mongoose.connection; | |
var routes = require('./routes/index'); | |
var users = require('./routes/users'); | |
// Init App | |
var app = express(); | |
// View Engine | |
app.set('views', path.join(__dirname, 'views')); | |
app.engine('handlebars', exphbs({ defaultLayout: 'layout', extname: '.handlebars' })); | |
app.set('view engine', 'handlebars'); | |
// BodyParser Middleware | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
// Set Static Folder | |
app.use(express.static(path.join(__dirname, 'public'))); | |
// Express Session | |
app.use(session({ | |
secret: 'secret', | |
saveUninitialized: true, | |
resave: true | |
})); | |
// Passport init | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// Express Validator | |
app.use(expressValidator({ | |
errorFormatter: function (param, msg, value) { | |
var namespace = param.split('.') | |
, root = namespace.shift() | |
, formParam = root; | |
while (namespace.length) { | |
formParam += '[' + namespace.shift() + ']'; | |
} | |
return { | |
param: formParam, | |
msg: msg, | |
value: value | |
}; | |
} | |
})); | |
// Connect Flash | |
app.use(flash()); | |
// Global Vars | |
app.use(function (req, res, next) { | |
res.locals.success_msg = req.flash('success_msg'); | |
res.locals.error_msg = req.flash('error_msg'); | |
res.locals.error = req.flash('error'); | |
res.locals.user = req.user || null; | |
next(); | |
}); | |
app.use('/', routes); | |
app.use('/users', users); | |
// Set Port | |
app.set('port', (process.env.PORT || 3000)); | |
app.listen(app.get('port'), function () { | |
console.log('Server started on port ' + app.get('port')); | |
}); |