-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (45 loc) · 1.28 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
require('dotenv').load();
const fs = require('fs');
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const Person = require('./server/person');
const route = require('./server/route');
if (process.env.MONGO_URI) {
mongoose.connect(process.env.MONGO_URI);
}
const app = express();
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.set('port', (process.env.PORT || 9000));
if (app.get('env') === 'development') {
app.use(require('connect-livereload')({ port: 35729 }));
app.use(morgan('combined'));
}
app.use(bodyParser.json());
app.use(express.static('public'));
app.use('/people', route);
app.get('/', (req, res) => {
if (process.env.MONGO_URI) {
Person.find((err, people) => {
if (err) { throw err; }
res.render('app', {
people: JSON.stringify(people),
layout: false,
});
return people;
});
} else {
fs.readFile('./people.json', { encoding: 'utf-8' }, (err, data) => {
res.render('app', {
people: data,
layout: false,
});
});
}
});
app.listen(app.get('port'), () => {
console.log('Listening on %d', app.get('port'));
});