-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
60 lines (51 loc) · 1.33 KB
/
server.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
import express from 'express';
import bodyParser from 'body-parser';
import 'dotenv/config';
import Logger from './Logger.js';
/**
* Import app routes
*/
import homepage from './server/routes/index.js';
import authRoute from './server/routes/auth.js';
import usersRoute from './server/routes/users.js';
import docsRoute from './server/routes/documents.js';
import docCategoryRoute from './server/routes/categories.js';
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/**
* Setup route middlewares.
*/
app.use('/', homepage);
app.use('/api/v1/auth', authRoute);
app.use('/api/v1/users', usersRoute);
app.use('/api/v1/templates', docsRoute);
app.use('/api/v1/categories', docCategoryRoute);
/**
* Handle unknown routes
*/
app.use((req, res) => {
res.status(404).json({
error: 'This page does not exist yet.'
});
});
/**
* Error handler
*/
app.use((err, req, res, next) => {
Logger.error(`error: ${err.message}`);
res.json({
error: err.reason || 'Internal server error',
message: err.message || 'Internal server error',
status: err.code || 500
});
next();
});
const port = process.env.PORT || 3000;
app.listen(port, (err) => {
if (err) {
Logger.error(`App failed to start.\n${err}`);
}
Logger.info(`App started on port ${port}`);
});
export default app;