-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
64 lines (55 loc) · 1.66 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Imports
if(process.env.NODE_ENV !== 'production'){
require('dotenv').config()
}
const express = require('express')
const expressLayouts = require('express-ejs-layouts')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
// Import Routes
const indexWebRoutes = require('./routes/web/index');
const paymentWebRoutes = require('./routes/web/payments');
// Create an instance of express app
const app = express()
// Set port
const port = process.env.PORT || '3000'
// Configure folders containing static files
app.use(express.static('public'));
app.use('/images', express.static(__dirname + 'public/images'));
// Configure Template Engine
app.use(expressLayouts)
app.set('layout', './layouts/default')
app.set('view engine', 'ejs')
// Configure bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
// Connect to MongoDB
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true })
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('[STATUS] Connected to Database'))
// Web Routes
app.use('/', indexWebRoutes);
app.use('/payment', paymentWebRoutes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// render the error page
res.status(err.status || 500);
res.render('pages/error', {
title: err.status,
status: err.status,
message: err.message
});
});
// Listen app on given port
app.listen(port, () => {
console.info(`[STATUS] App listening on port ${port}`)
})