之前遇到一个需要本地模拟跨域现象的需求,苦于无法将前端项目以服务启动,然后在网上了这个,以nodejs方式启动。

node server.js

server.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
var http = require('http');
var fs = require('fs');//引入文件读取模块

var documentRoot = '';
//需要访问的文件的存放目录(项目所在位置的文件夹路径)

var server= http.createServer(function(req,res){

var url = req.url;
//客户端输入的url,例如如果输入localhost:8888/index.html
//那么这里的url == /index.html

var file = documentRoot + url;
console.log(url);
//E:/PhpProject/html5/websocket/www/index.html


fs.readFile( file , function(err,data){
/*
一参为文件路径
二参为回调函数
回调函数的一参为读取错误返回的信息,返回空就没有错误
二参为读取成功返回的文本内容
*/
if(err){
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
res.end();
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write(data);//将index.html显示在客户端
res.end();

}

});



}).listen(8081);

console.log('服务器开启成功');


node

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!