-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (41 loc) · 1.1 KB
/
app.js
File metadata and controls
49 lines (41 loc) · 1.1 KB
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
// imports
const express = require('express');
const createErrors = require('http-errors');
const userRoute = require('./routes/user.route');
const categoryRoute = require('./routes/category.route');
const blogRoute = require('./routes/blog.route');
const cors = require('cors');
// constants
const app = express();
app.use('/uploads', express.static('uploads'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({
origin: '*'
}));
// routes
app.get('/', (req, res) => {
res.send('Hello heroku');
});
app.use('/user', userRoute);
app.use('/category', categoryRoute);
app.use('/blog', blogRoute);
// handle wildcard route
app.use(async(req, res, next) => {
next(createErrors.NotFound('This route does not exists!'));
});
// handle errors
app.use((err, req, res, next) => {
if (err.code === 'LIMIT_FILE_SIZE') {
err.status = 400;
}
res.status(err.status || 500);
res.send({
error: {
status: err.status || 500,
message: err.message || 'Internal server error'
}
});
});
// exports
module.exports = app;