forked from wuyawei/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (63 loc) · 1.81 KB
/
Copy pathserver.js
File metadata and controls
73 lines (63 loc) · 1.81 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
/**
* Created by wyw on 2018/10/14.
*/
// 导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示:
const Koa = require('koa');
const path = require('path');
const koaSend = require('koa-send');
const static = require('koa-static');
const fs = require('fs');
const https = require('https');
// 创建一个Koa对象表示web app本身:
const app = new Koa();
app.use(static(
path.join( __dirname, './public')
));
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
// response
// 对于任何请求,app将调用该异步函数处理请求:
app.use((ctx, next) => {
let query = ctx.query;
ctx.body = `${query['callback']}('wuyw')`;
next();
});
// app.use(async (ctx, next) => {
// if (/\./.test(ctx.request.url)) {
// await koaSend(
// ctx,
// 'index.html',
// {
// root: path.join(__dirname, './'),
// maxage: 1000 * 60 * 60 * 24 * 7,
// gzip: true,
// } // eslint-disable-line
// );
// } else {
// await next();
// }
// });
// 在端口3001监听:
/*app.listen(3001, _ => {
console.log('app started at port 3001...');
});*/
let options = {
key: fs.readFileSync('./privatekey.pem'),
cert: fs.readFileSync('./certificate.pem')
};
// https协议需要生成相关证书
https.createServer(options, app.callback()).listen(3001, _ => {
console.log('app started at port 3001...');
});
// https.createServer(app.callback()).listen(3001);