-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
65 lines (56 loc) · 1.65 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
const express = require('express'),
jsonParser = require('body-parser').json,
app = express(),
router = require('./routes.js'),
logger = require('morgan'),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
sanitizer = require('express-sanitizer');
mongoose.Promise = global.Promise; //Use native promises instead of deprecated mpromise
mongoose.connect("mongodb://localhost:27017/qa")
const db = mongoose.connection;
db.on('error', (err) => {
console.error("connection error:", err)
});
db.once('open', () => {
console.log('db connection successful');
});
app.use((request, response, next) => {
response.header({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
});
if (request.method === "OPTIONS") {
response.header({
"Access-Control-Allow-Methods": "PUT,POST,DELETE"
});
return response.status(200).json({});
}
next();
});
app.use(logger('dev'));
app.use(jsonParser());
app.use(sanitizer());
app.use((request, response, next) => {
request.body.text = request.sanitize(request.body.text);
next();
});
app.use('/questions', router);
app.use(errorRoute);
app.use(handleErrors);
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
function errorRoute(request, response, next) {
var error = new Error("Not Found");
error.status = 404;
next(error);
}
function handleErrors(error, request, response, next) {
response.status(error.status || 500);
response.json({
error: {
message: error.message
}
});
}