-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (29 loc) · 990 Bytes
/
server.js
File metadata and controls
37 lines (29 loc) · 990 Bytes
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
import http from 'http';
//自定义
import router from './src/router';
http.createServer(function (req, res) {
// 过滤favicon.ico
if (req.url == "/favicon.ico") {
res.writeHead(204);
res.end();
return;
}
if (req.headers.origin) {
// 跨域配置
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'token, content-type');
// 如果客户端带cookie时,需要指定 Access-Control-Allow-Origin, Access-Control-Allow-Credentials
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Credentials', true);
}
// 浏览器预解析请求拦截
if (req.method == 'OPTIONS') {
res.writeHead(204, { 'Content-Length': 0 });
res.end();
return;
}
// 入口
router(req, res);
}).listen('8888');
console.log('服务已启动 http://localhost:%d', 8888);