-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-interceptor.js
More file actions
60 lines (46 loc) · 1.35 KB
/
http-interceptor.js
File metadata and controls
60 lines (46 loc) · 1.35 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
const Server = require('./lib/interceptor-server');
const mime = require('mime');
const url = require('url');
const parseQuery = require('./lib/middleware/query');
const router = require('./lib/middleware/router');
function getMimeType(res) {
const EXT_MIME_TYPES = mime.types;
const path = require('path');
const mime_type = EXT_MIME_TYPES[path.extname(res).slice(1) || 'html'];
return mime_type;
}
const app = new Server();
app.use((context, next) => {
console.log(`Visit: ${context.req.url}`);
next();
});
app.use(async (context, next) => {
const req = context.req;
const srvUrl = url.parse(`http://${req.url}`);
let path = srvUrl.path;
if(path === '/') path = '/index.html';
const resPath = `resource${path}`.replace(/\?.*/g, '');
const mimeType = getMimeType(resPath);
context.mimeType = mimeType;
await next();
});
// app.use(parseQuery);
// app.use((context, next) => {
// context.status = 200;
// context.body = `<h1>Hello World!</h1>${JSON.stringify(context.query)}`;
// });
const index = router.get('/', (context, next) => {
context.status = 200;
context.body = 'Hello';
next();
});
const test = router.get('/test', (context, next) => {
context.status = 200;
context.body = 'Test';
next();
});
app.use(index);
app.use(test);
app.listen(10080, (server) => {
console.log('opened server on', server.address());
});