forked from klgvillanueva/Tech-Talk-React-Router-Demo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (37 loc) · 1.06 KB
/
server.js
File metadata and controls
45 lines (37 loc) · 1.06 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
const express = require('express');
const path = require('path');
const app = express();
const PORT = 3000;
// flow check
app.use((req, res, next) => {
console.log(`
🍒🍒🍒 FLOW METHOD 🍒🍒🍒
URL: ${req.url}\n
METHOD: ${req.method}\n`);
return next();
});
/*** MAIN PAGE ***/
app.use(express.static(path.resolve(__dirname, './public')));
/* to handle React Router */
app.get('/*',
(req, res) => {res.sendFile(path.join(__dirname, '../public/index.html'))}
);
// catch all
app.use('*', (req, res, next) => {
return res.status(404).send('Sorry, wrong page! Try again! 🤪');
});
// default error handler
app.use((err, req, res, next) => {
const defaultErr = {
log: 'Express error handler caught unknown middleware error',
status: 500,
message: { err: 'Internal Server Error' },
};
const errorObj = { ...defaultErr, ...err };
console.log(errorObj.log);
return res.status(errorObj.status).json(errorObj.message);
});
app.listen(PORT, () => {
console.log(`👀 Server listening on port: ${PORT}... 👀 `);
});
module.exports = app;