const http = require("http");
const fs = require("fs");
const server = http.createServer(function(req,res){
//设置响应头
res.writeHead(200,{"Content-Type":"text/html;charset=UTF-8"})
//设置访问的url路径
if(req.url == "/" || req.url == "/index"){
//路径为相对路径,也就是说与这个js文件同级的有一个html文件
fs.readFile("index.html",function(err,data){
res.writeHead(200,{"Content-Type":"text/html;charset=UTF-8"});
res.end(data);
});
}
})
server.listen(8080,function(){
console.log('running at 8080......');
}
node.js 实现读取文件
原创
©著作权归作者所有:来自51CTO博客作者前端面试题库助手的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
node.js 读取json文件
node.js 读取json文件
node.js 读取json json html -
Node.js文件的读取
1上新建一个test1.txt,里面书写:readfile success!2新建re
node.js文件的读取 desktop 读取文件 -
【Node.JS】读取文件内容
fs模块是Node.js官方提供的,用来操作文件的模块,它提供了一系列的方法和属性,用来满足用
node.js javascript 操作文件 文件系统 回调函数 -
Node.js删除文件
Node.js删除文件。
node.js 删除文件 文件系统 -
node.js 读取yaml文件
node.js 读取yaml文件1、安装yamljs包,具体的安转命令为:sudo npm inastall -g yamljs2、编写名为'manifest.yml'的yaml配置文件Config:Srvc: ...
json ide node.js javascript sed