-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (44 loc) · 1.18 KB
/
server.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
40
41
42
43
44
45
46
47
48
49
50
51
require('dotenv').config();
const express = require('express');
const path = require('path');
const uuid = require('uuid');
const bodyParser = require('body-parser');
const {
unsplashApi,
apiNotFound,
jwtGet,
jwtTest,
loginRouter,
} = require('./routes');
console.log(`server root set to: \n${path.join(__dirname + '/build')}`);
const PORT = process.env.PORT;
const app = express();
app.use(logger);
app.use(bodyParser.json());
app.use('/static', express.static('./build/static'));
app.get('/api/unsplash/*', unsplashApi);
app.post('/login', loginRouter);
app.get('/jwt/get', jwtGet);
app.get('/jwt/test', jwtTest);
app.get('/api*+', apiNotFound);
// SPA
app.get('/*', (req, res) => {
console.log(`[${req.uuid}] -SPA redirect- `);
res.sendFile(path.join(__dirname + '/build/index.html'));
});
app.all('*', (req, res) => {
console.log(`[${req.uuid}] -catch all route- `);
res.status(405);
res.set({ Allow: 'GET' });
res.send('<p>Method Not Supported</p>');
});
// LOGGER
function logger(req, res, next) {
req.uuid = uuid.v4();
console.log(`[${req.uuid}] ${req.method} to ${req.url}`);
next();
}
// INIT
app.listen(PORT, () => {
console.log('listening to port ' + PORT);
});