-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.22 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
40
41
42
43
44
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const consign = require('consign');
const jwt = require('jsonwebtoken');
require('dotenv').load();
const port = process.env.SERVER_PORT;
app.set('jwt', jwt);
app.use(bodyParser.json()); // middleware
app.use('*', (req, resp, next) => {
resp.setHeader('Access-Control-Allow-Origin', '*')
resp.setHeader('Access-Control-Allow-Headers', 'Content-Type')
if (req.originalUrl == '/auth' || req.originalUrl == '/health') {
next();
} else {
const token = req.headers['x-access-token'] || req.query.access_token;
if (!token) {
resp.status(401).end();
} else {
jwt.verify(token, process.env.JWT_KEY, (err, decoded) => {
if (err) {
resp.status(401).end();
} else {
req.user = decoded.user;
next();
}
})
}
}
})
consign({ cwd: 'src' })
.include('controller')
.then('repository')
.then('db')
.into(app);
app.get('/health', (req, res) => res.json({ status: 'UP' }));
app.listen(port, () => console.log(`App listening on port ${port}!`));