-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverkoa.js
More file actions
116 lines (90 loc) · 3.38 KB
/
serverkoa.js
File metadata and controls
116 lines (90 loc) · 3.38 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const koa = require('koa2')
const serve = require('koa2-static-middleware');
var serveStatic = require('koa-static-folder');
const Router = require("koa-router");
const app = new koa();
const router = new Router();
const BodyParser = require("koa-bodyparser");
let request = require('koa2-request');
const send = require('koa-send');
const { httpProxy } = require('koa-http-proxy-middleware-fix');
const httpsProxyAgent = require('https-proxy-agent');
const microServicePath = "http://localhost:6002";
const wsMicroServicePath = "ws://localhost:6002";
path = require('path'),
fs = require('fs');
var staticRoot = __dirname + '/';
app.use(httpProxy('/api', {
target: microServicePath,
changeOrigin: true,
// rewrite: path => path.replace(/^\/api(\/|\/\w+)?$/, '/api'),
logs: true
}))
app.use(httpProxy('/uploads', {
target: microServicePath,
changeOrigin: true,
// rewrite: path => path.replace(/^\/api(\/|\/\w+)?$/, '/api'),
logs: true
}))
app.use(httpProxy('/angular', {
target: microServicePath,
changeOrigin: true,
// rewrite: path => path.replace(/^\/api(\/|\/\w+)?$/, '/api'),
logs: true
}))
app.use(httpProxy('/socket.io', {
target: wsMicroServicePath,
changeOrigin: true,
// rewrite: path => path + "/socket.js",
logs: true
}))
app.use(async function(ctx, next) {
// console.log(ctx);
// console.log(ctx.path);
if (ctx.path == '/') {
return await next();
}
// console.log('b');
var ext = path.extname(ctx.path);
// console.log("ext = " + ext);
if (ext !== '') {
// console.log('aaaaaaaaaaaaaaa');
await send(ctx, 'dist/' + ctx.path);
}
// console.log("ccccccccccccccccccccccccc");
let resp = null;
try {
resp = await next();
// console.log(resp);
return resp;
}
catch (exxx){
// console.log(exxx);
const root = path.resolve("./dist");
let requested = path.normalize("/index.html");
return await send(ctx, requested, { root });
}
// return await send(ctx, '/dist/index.html');
})
router.get('/', serve('./dist', { index: 'index.html' }));
router.get('/home', serve('./dist', { index: 'index.html' }));
router.get('/dailyNews', serve('./dist', { index: 'index.html' }));
router.get('/login', serve('./dist', { index: 'index.html' }));
router.get('/dailyChallenge', serve('./dist', { index: 'index.html' }));
router.get('/survey', serve('./dist', { index: 'index.html' }));
router.get('/courses', serve('./dist', { index: 'index.html' }));
router.get('/registercourse', serve('./dist', { index: 'index.html' }));
router.get('/about', serve('./dist', { index: 'index.html' }));
router.get('/createUser', serve('./dist', { index: 'index.html' }));
router.get('/forgotPassword', serve('./dist', { index: 'index.html' }));
router.get('/resetpassword', serve('./dist', { index: 'index.html' }));
router.get('/addquestions', serve('./dist', { index: 'index.html' }));
router.get('/users', serve('./dist', { index: 'index.html' }));
router.get('/edituser', serve('./dist', { index: 'index.html' }));
router.get('/confirmemail', serve('./dist', { index: 'index.html' }));
router.get('/js', serve('./dist', { index: 'index.html' }));
router.get('/addNews', serve('./dist', { index: 'index.html' }));
router.get('/*', serve('./dist', { index: 'index.html' }));
app.use(router.routes()).use(router.allowedMethods());
const port = 80;
app.listen(port, () => console.log(`==> Listening at http://localhost:${port}`));