forked from cmda-minor-web-cases/coding-the-curbs-2122
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (40 loc) · 1.19 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
// IMPORT MODULES
import express from 'express'
const app = express()
import compression from 'compression'
import bodyParser from 'body-parser'
import 'dotenv/config.js'
import {router} from './router/router.js'
import {connectDB} from './modules/mongoClient.js'
// SETTINGS GLOBAL VARIABLES
const port = process.env.PORT || 1234
// LINKING TEMPLATE ENGINE TO EXPRESS
app.set('view engine', 'ejs')
// TELLING EJS WHERE THE VIEWS ARE
app.set('views', 'views')
// SETTING CACHE CONTROL
app.use(/.*-[0-9a-f]{10}\..*/, (req, res, next) => {
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
next()
})
// INITIALIZE STATIC FOLDER, COMPRESSION, BODYPARSER AND ROUTER
app.use(express.static('static'))
app.use(compression())
app.use(bodyParser())
app.use(router)
// MAKE CONNECTION TO DATABASE MODULE
connectDB()
app.listen(port, () => {
console.log(`Example app listening on port http://localhost:${port}`)
})
app.use(function(req, res, next) {
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('error', { url: req.url,title:"There is an error" });
return;
}else{
// default to plain-text. send()
res.type('txt').send('Not found');
}
});