-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (32 loc) · 1.21 KB
/
Copy pathserver.js
File metadata and controls
46 lines (32 loc) · 1.21 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
const express = require('express');
const cors = require('cors');
const { getApi, getTopics, getArticleById, patchArticleById, getUsers, getArticles, getCommentsByArticleId, postCommentByArticleId, getArticlesWithQueries } = require('./controllers/controller');
const app = express();
app.use(cors());
app.use(express.json());
app.get('/api', getApi);
app.get('/api/topics', getTopics);
app.get('/api/articles', getArticles)
app.get('/api/articles/:article_id', getArticleById);
app.patch('/api/articles/:article_id', patchArticleById);
app.get('/api/articles/:article_id/comments', getCommentsByArticleId);
app.get('/api/users', getUsers);
app.post('/api/articles/:article_id/comments', postCommentByArticleId)
// Bad path error handler
app.use('*', (request, response) => {
response.status(404).send({ message: '404: path not found' });
})
// Custom error handler
app.use((error, request, response, next) => {
const { status, message } = error;
if (status) {
response.status(status).send({ message });
} else {
next(error);
}
})
// 500 error handler
app.use((error, request, response, next) => {
response.status(500).send({ message: '500: server error' })
})
module.exports = app;