-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (53 loc) · 1.54 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
const express = require('express');
const bodyParser = require('body-parser');
const logger = require("morgan");
const auth = require('./middleware/auth');
const app = express();
///Init Startup files
require('./startup/config');
require('./startup/dbconfig');
app.use(logger("dev"));
// parse application/x-www-form-urlencoded
app.use(
bodyParser.urlencoded({
extended: false
})
);
// parse application/json
app.use(bodyParser.json())
/* ***************** Index Route ************ */
app.get("/", (req, res) => {
res.send('This is the index page of this server');
});
//public route
app.use('/users', require('./routes/users'));
// private route
app.use("/movies", auth, require('./routes/movies'));
app.get("/favicon.ico", (req, res) => {
res.sendStatus(204);
});
// express doesn't consider not found 404 as an error so we need to handle 404 explicitly
// handle 404 error
app.use((req, res, next) => {
let err = new Error("Not Found");
err.status = 404;
next(err);
});
// handle errors
app.use((err, req, res, next) => {
console.log(err);
if (err.status === 404)
res.status(404).json({
message: "Not found"
});
else
res.status(500).json({
message: "Something looks wrong :( !!!"
});
});
/* ************* Start Listening **************** */
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.listen(app.get('port'), () => {
console.log("Node app is running at localhost:" + app.get('port'))
});