nodejs写一个简单的Web服务器

目录文件如

httpFile.js如下:

const httpd = require("http");
const fs = require("fs");

//创建服务
httpd.createServer((req,res)=>{
    //读取www文件夹下的路径
    fs.readFile(`www${req.url}`,(err,data)=>{
        //失败返回404
        if(err){
            res.write('<h1>404</h1>');
        }else{
            //成功返回页面
            res.write(data);
        }
        res.end();
    })
}).listen(8082);
console.log('runing......')

访问

访问1.html

 完成!!

猜你喜欢

转载自www.cnblogs.com/y-y-y-y/p/10528044.html