node.js写的一个五子棋 怎么访问html页面
2016-05-03 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
参考下面代码:
var http = require(‘http’);
var fs = require(‘fs’);
var path = require(‘path’);
var util = require(‘util’);
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './ws.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
var ifbinary = false;
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.gif':
contentType = 'image/gif';
ifbinary = true;
break;
case '.png':
contentType = 'image/png';
ifbinary = true;
break;
case '.jpg':
contentType = 'image/jpeg';
ifbinary = true;
break;
}
path.exists(filePath, function(exists) {
if (exists) {
if (ifbinary) {
fs.stat(filePath, function(error, stat) {
var rs;
response.writeHead(200,{ 'Content-Type': contentType, 'Content-Length' : stat.size });
rs = fs.createReadStream(filePath);
util.pump(rs, response, function(err) {
if(err) {
throw err;
}
});
});
}
else {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(8125);
var http = require(‘http’);
var fs = require(‘fs’);
var path = require(‘path’);
var util = require(‘util’);
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './ws.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
var ifbinary = false;
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.gif':
contentType = 'image/gif';
ifbinary = true;
break;
case '.png':
contentType = 'image/png';
ifbinary = true;
break;
case '.jpg':
contentType = 'image/jpeg';
ifbinary = true;
break;
}
path.exists(filePath, function(exists) {
if (exists) {
if (ifbinary) {
fs.stat(filePath, function(error, stat) {
var rs;
response.writeHead(200,{ 'Content-Type': contentType, 'Content-Length' : stat.size });
rs = fs.createReadStream(filePath);
util.pump(rs, response, function(err) {
if(err) {
throw err;
}
});
});
}
else {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(8125);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询