-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
45 lines (35 loc) · 1.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var process = require('process');
var express = require('express');
var handlebars = require('express-handlebars');
var handlebarsSections = require('express-handlebars-sections');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
var models = require('./models');
var hbs = handlebars.create({defaultLayout: 'main', partialsDir: ['views/partials/']});
handlebarsSections(hbs);
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// connect to mongodb
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});
function wantsJson() {
return this.accepts('html', 'json') === 'json';
}
function smartRender(req, res, err, data, view) {
if (err)
return res.send(err);
if(req.wantsJson())
return res.json(data);
else
return res.render(view, data);
}
app.use(function (req, res, next) {
req.wantsJson = wantsJson;
res.smartRender= smartRender;
next();
});
var routes = require('./routes')(app);
app.listen(process.env.PORT || 3000)