-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (30 loc) · 1.01 KB
/
app.js
File metadata and controls
43 lines (30 loc) · 1.01 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
const express = require('express');
const AuthRoutes = require('./routes/auth.routes');
const PostRoutes = require('./routes/post.routes');
const app = express();
app.use(express.json())
// define routes
app.use('/auth', AuthRoutes);
app.use('/posts', PostRoutes)
app.get('/', (req, res) => {
// res.json({ message: 'Welcome to Feed App Api'})
res.json({ message: 'Welcome to Feed App Api'})
})
// Handle asynchronous error using error middleware
app.use((error, req, res, next) => {
console.log("Error Handling Middleware called")
console.log('Path: ', req.path)
console.error('Error: ', error)
if (error.type == 'NOT_FOUND')
res.status(500).send(error)
else if (error.type == 'Not Found') // arbitrary condition check
res.status(404).send(error)
else {
res.status(500).send(error)
}
next() // next is required to call next middleware if any
})
app.get('*', (req, res) => {
res.json({ message: 'Route not found', code: 404 })
})
module.exports = app;