-
Notifications
You must be signed in to change notification settings - Fork 118
/
app.js
80 lines (65 loc) · 2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict'
const express = require('express')
const timeout = require('connect-timeout')
const path = require('path')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const AV = require('leanengine')
// Loads cloud function definitions.
// You can split them into several files, but don't forget to load them into the main file.
require('./cloud')
const app = express()
// Configures template engine.
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// Configures default timeout.
app.use(timeout('15s'))
// Loads LeanEngine middleware.
app.use(AV.express())
app.enable('trust proxy')
// Uncomment the following line to redirect all HTTP requests to HTTPS.
// app.use(AV.Cloud.HttpsRedirect())
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.get('/', (req, res) => {
res.header('Cache-Control', 'no-cache')
res.render('index', { currentTime: new Date() })
})
// You can put routings in multiple files according to their categories.
app.use('/todos', require('./routes/todos'))
app.use((req, res, next) => {
// If there is no routing answering, throw a 404 exception to exception handlers.
if (!res.headersSent) {
const err = new Error('Not Found')
err.status = 404
next(err)
}
})
// error handler
app.use((err, req, res, next) => {
if (req.timedout && req.headers.upgrade === 'websocket') {
// Ignores websocket timeout.
return
}
const statusCode = err.status || 500
if (statusCode === 500) {
console.error(err.stack || err)
}
if (req.timedout) {
console.error('Request timeout: url=%s, timeout=%d, please check whether its execution time is too long, or the response callback is invalid.', req.originalUrl, err.timeout)
}
res.status(statusCode)
// Do not output exception details by default.
let error = {}
if (app.get('env') === 'development') {
// Displays exception stack on page if running in the development enviroment.
error = err
}
res.render('error', {
message: err.message,
error: error
})
})
module.exports = app