-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (39 loc) · 1.16 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
45
46
47
48
49
50
51
52
53
54
const express = require("express");
const cors = require("cors");
const helmet = require('helmet');
const jwt = require('jsonwebtoken');
const restricted = require('./middleware/restricted-middleware');
const authRouter = require('./routers/authRouter');
const userRouter = require('./routers/userRouter');
const server = express();
const port = process.env.PORT || 5000
server.use(helmet());
server.use(cors());
server.use(express.json());
server.use('/api/auth', authRouter);
server.use('/api/users', restricted, userRouter);
server.get("/", (req, res) => {
res.json({message: "Welcome to the server!"})
})
server.get('/token', (req, res) => {
const payload = {
subject: "thisuser",
userid: 'dpinkett',
color: 'purple'
}
const secret = 'privatekey';
const options = {
expiresIn: '30m'
}
const token = jwt.sign(payload, secret, options);
res.json(token);
})
server.use((err, req, res, next) => {
res.status(500).json({message: "Something went worng"})
})
if(!module.parent) {
server.listen(port, () => {
console.log(`Running at http://localhost:${port}`)
})
}
module.exports = server