如何用nodejs打开项目中的html文件?
刚学node,遇到一个问题:假设项目目录(D:test)下只有run.js和index.html文件,我希望用node运行run.js的时候,在浏览器访问localhos...
刚学node,遇到一个问题:
假设项目目录(D:test)下只有run.js和index.html文件,我希望用node运行run.js的时候,在浏览器访问localhost:8888能够访问index.html文件的内容,应该怎么操作? 展开
假设项目目录(D:test)下只有run.js和index.html文件,我希望用node运行run.js的时候,在浏览器访问localhost:8888能够访问index.html文件的内容,应该怎么操作? 展开
2个回答
展开全部
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");
http.createServer(function (req, res) {
var pathname=__dirname+url.parse(req.url).pathname;
if (path.extname(pathname)=="") {
pathname+="/";
}
if (pathname.charAt(pathname.length-1)=="/"){
pathname+="index.html";
}
path.exists(pathname,function(exists){
if(exists){
switch(path.extname(pathname)){
case ".html":
res.writeHead(200, {"Content-Type": "text/html"});
break;
case ".js":
res.writeHead(200, {"Content-Type": "text/javascript"});
break;
case ".css":
res.writeHead(200, {"Content-Type": "text/css"});
break;
case ".gif":
res.writeHead(200, {"Content-Type": "image/gif"});
break;
case ".jpg":
res.writeHead(200, {"Content-Type": "image/jpeg"});
break;
case ".png":
res.writeHead(200, {"Content-Type": "image/png"});
break;
default:
res.writeHead(200, {"Content-Type": "application/octet-stream"});
}
fs.readFile(pathname,function (err,data){
res.end(data);
});
} else {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}).listen(8080, "127.0.0.1");
console.log("Server running at http://127.0.0.1:8080/");
url = require("url"),
path = require("path"),
fs = require("fs");
http.createServer(function (req, res) {
var pathname=__dirname+url.parse(req.url).pathname;
if (path.extname(pathname)=="") {
pathname+="/";
}
if (pathname.charAt(pathname.length-1)=="/"){
pathname+="index.html";
}
path.exists(pathname,function(exists){
if(exists){
switch(path.extname(pathname)){
case ".html":
res.writeHead(200, {"Content-Type": "text/html"});
break;
case ".js":
res.writeHead(200, {"Content-Type": "text/javascript"});
break;
case ".css":
res.writeHead(200, {"Content-Type": "text/css"});
break;
case ".gif":
res.writeHead(200, {"Content-Type": "image/gif"});
break;
case ".jpg":
res.writeHead(200, {"Content-Type": "image/jpeg"});
break;
case ".png":
res.writeHead(200, {"Content-Type": "image/png"});
break;
default:
res.writeHead(200, {"Content-Type": "application/octet-stream"});
}
fs.readFile(pathname,function (err,data){
res.end(data);
});
} else {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}).listen(8080, "127.0.0.1");
console.log("Server running at http://127.0.0.1:8080/");
展开全部
如果非要用nodejs打开就这么写
function detail(response, query_param){
fs.readFile('./sina_weibo.html','utf-8',function(err, data) {//读取内容
if(err) throw err;
response.setHeader('content-type', 'text/html;charset=utf-8');
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(data);
response.end();
});
}
建议你用exress框架里面直接封装好方法直接response.render('path');
function detail(response, query_param){
fs.readFile('./sina_weibo.html','utf-8',function(err, data) {//读取内容
if(err) throw err;
response.setHeader('content-type', 'text/html;charset=utf-8');
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(data);
response.end();
});
}
建议你用exress框架里面直接封装好方法直接response.render('path');
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询