Node.js创建本地简易服务器

创建简易的本地服务器

  • 安装node.js
  • 在项目下,通过npm init -y创建package.json文件
  • 通过npm install mime --save加载mime插件
  • 创建server.js·,内容如下
    ```var http=require("http");
    var path=require("path");
    var fs = require("fs");
    var mime = require("mime");

    http.createServer(function(req,res){
    var publicDir = path.join(__dirname);
    var filename = path.join(publicDir,req.url);
    // 控制台输出路径
    // console.log("filename ==> "+filename)
    fs.readFile(filename,function(err,data){
    if (err) {
    // 处理中文乱码
    res.writeHeader(200, {'Content-Type': 'text/html;charset=utf-8'});
    res.end("文件不存在 404 ");
    }else{
    // 统一获取资源
    res.setHeader('Content-Type', mime.getType(filename));
    res.end(data);
    }
    });

    }).listen(9090,function(){
    // 端口号9090
    console.log("http://localhost:9090");
    });```

猜你喜欢

转载自www.cnblogs.com/AuKing/p/10289841.html