-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn5_readfile.js
52 lines (48 loc) · 2.14 KB
/
n5_readfile.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
43
44
45
46
47
48
49
50
51
52
var http = require('http');
var url = require('url');
var router = require('./modules/router');
/*
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/html; charset = utf-8'}); //http协议头
if(request.url !== "/favicon.ico"){ //清除node的第二次访问,有些高级框架已清除
// optfile.readfileSync('./views/login.html');
// 闭包,解决先后冲突的问题
function recall(data){
response.write(data);
response.end('OK');
}
// optfile.readfile('./views/login.html', response);
// 闭包方式
optfile.readfile('./views/login.html', recall);
// response.end('OK') //协议尾,结束本次http协议
console.log('主程序执行完毕');
}
}).listen(8000);
*/
//修改程序后将上述内容修改如下:
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/html; charset = utf-8'}); //http协议头
if(request.url !== "/favicon.ico"){ //清除node的第二次访问,有些高级框架已清除
var pathname = url.parse(request.url).pathname;
// console.log(pathname);
pathname = pathname.replace(/\//, ''); //替换掉前面的'/'
// 在router.js里面只写了login和register方法,没写异常处理,
// 所以浏览器直接访问 http://127.0.0.1:8000/login 或者 http://127.0.0.1:8000/register 没有问题,
// 访问其他路由后台崩溃。
router[pathname](request, response);
/*
// optfile.readfileSync('./views/login.html');
// 闭包,解决先后冲突的问题
function recall(data){
response.write(data);
response.end('OK');
}
// optfile.readfile('./views/login.html', response);
// 闭包方式
optfile.readfile('./views/login.html', recall);
// response.end('OK') //协议尾,结束本次http协议
console.log('主程序执行完毕');
*/
}
}).listen(8000);
console.log('在浏览器中访问 http://127.0.0.1:8000/login 或者 http://127.0.0.1:8000/register');