-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (57 loc) · 1.63 KB
/
server.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
58
59
60
61
62
63
64
65
66
'use strict';
/*
* CRUD application using express and mongodb
* Book Inventory
*/
// Express init
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const port = 3000;
const bodyParser = require('body-parser');
const config = require('./config.js');
var hbs = require('express-handlebars').create({
layoutsDir: './app/views/layouts/',
defaultLayout: 'main',
extname: '.hbs',
helpers: require('./helpers.js'),
compilerOptions: {}
});
// Env
app.set('x-powered-by', false)
app.set('views', __dirname + '/app/views');
app.use(express.static('public/'));
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// Import Models
require('./app/models/authors');
require('./app/models/books');
require('./app/models/publishers');
// Routes
var books = require('./app/routes/books');
var publishers = require('./app/routes/publishers');
var authors = require('./app/routes/authors');
app.use('/book', books);
app.use('/publisher', publishers);
app.use('/author', authors);
app.get('/', function (req, res) {
res.render('home');
});
app.get('/favicon.ico', function (req, res) {
res.send('\n');
});
// Main loop
connect()
.on('error', console.log)
.on('disconnected', connect)
.once('open', listen);
function listen () {
app.listen(port);
console.log('Express app started on port ' + port);
}
function connect () {
var options = {server: {socketOptions: {keepAlive: 1}}};
return mongoose.connect(config.db, options).connection;
}