-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
42 lines (35 loc) · 1.09 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
import Koa from 'koa';
import Router from 'koa-router';
import storage from 'node-persist';
import path from 'path';
import serve from 'koa-static';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';
import ip from 'ip'
import dotenv from 'dotenv';
dotenv.config();
const __dirname = path.resolve();
const port = process.env.HOST_PORT || 3000
await storage.init()
const router = new Router();
router.get('/logout', async (ctx, _) => {
await storage.removeItem('token');
ctx.response.redirect('/login.html');
});
router.post('/token', async (ctx, next) => {
await storage.setItem('token', ctx.request.body.token);
ctx.response.status = 200;
});
router.get('/token', async (ctx, _) => {
const userToken = await storage.getItem('token');
const devToken = process.env.DEV_TOKEN;
const hostUrl = `http://${ip.address()}:${port}/login.html`
ctx.body = {userToken, devToken, hostUrl};
});
const app = new Koa();
app.use(cors())
.use(serve(__dirname + '/public'))
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
app.listen(port);