-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (35 loc) · 1.15 KB
/
index.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
//library/package imports
const express = require('express');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv').config();
//local/module imports
const authRouter = require('./routes/authRouter');
const postRouter = require('./routes/postRouter');
const friendsRouter = require('./routes/friendsRouter');
//app setup
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
//route handling
app.use('/post', postRouter);
app.use('/auth', authRouter);
app.use('/friends', friendsRouter);
app.get('/', (req, res) => {
return res.status(200).json('Hello World');
});
//global error handler
app.use((err, req, res, next) => {
const defaultErr = {
log: 'Express error handler caught unknown middleware error',
status: 400,
message: { error: 'An error occurred' },
};
const errorObj = Object.assign(defaultErr, err);
console.log(errorObj.log);
return res.status(errorObj.status).json(errorObj.message);
});
//server setup
const server = app.listen(3000 || process.env.BACKEND_PORT, () => {
console.log('Server started on port ' + process.env.BACKEND_PORT);
});