1 Star 0 Fork 0

Bluishoul/grunt-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.js 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
Bluishoul 提交于 2013-11-21 22:05 . socket.io demo
var http = require('http');
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
response.charset = 'utf-8';
var t = new Date();
var pathname = url.parse(request.url).pathname;
var realPath = '.'+pathname;
if(realPath.length<=2){
realPath = './index.html';
}
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
} else {
if(realPath.indexOf('.js')!=-1){
response.writeHead(200, {'Content-Type': 'text/javascript'});
}else if(realPath.indexOf('.css')!=-1){
response.writeHead(200, {'Content-Type': 'text/css'});
}else if (realPath.indexOf('.html')!=-1) {
response.writeHead(200, {'Content-Type': 'text/html'});
}else{
response.writeHead(200, {'Content-Type': 'text/plain'});
};
response.write(file, "binary");
response.end();
}
});
}
});
console.log('['+t.toLocaleString()+']'+pathname);
});
server.listen(8080);
console.log("server start up at http://localhost:8080\n");
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/bluishoul/grunt-demo.git
git@gitee.com:bluishoul/grunt-demo.git
bluishoul
grunt-demo
grunt-demo
master

搜索帮助