generated from NCKU-CCS/digital-certificate-ncku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (35 loc) · 1.23 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
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const jwt = require('jsonwebtoken');
const next = require('next');
dotenv.config();
const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT) || 3000;
const jwtsecret = process.env.JWT_SECRET || '';
const app = next({ dev });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
server.get('/', (req, res) => app.render(req, res, '/'));
server.get('/support', (req, res) => app.render(req, res, '/support'));
server.get('/upload', (req, res) => app.render(req, res, '/upload'));
server.get('/descript', (req, res) => app.render(req, res, '/descript'));
server.get('/api/token', (_, res) => {
const token = jwt.sign(
{
from: 'digital-certificate-edu',
expired: Date.now() + 1000 * 60 * 10,
},
jwtsecret,
);
res.send(token);
});
server.all('*', (req, res) => handler(req, res));
server.listen(port, err => {
if (err) throw err;
if ('' === jwtsecret) throw Error('Token null or undefined');
console.info(`Start on ${port}`);
});
});