Node.js 实现下载

//html
下载
//服务器
var http = require("http");
var fs = require("fs");
http.createServer((req,res)=>{
    if(req.url == "/favicon.ico"){
        return;
    }
    if(req.url == "/"){
        fs.readFile("./index.html",(err,data)=>{
            res.writeHead(200,{"Content-type":"text/html;charset=utf-8"})
            res.end(data)
        })
    }
    if(req.url == "/downData"){
        fs.readFile("./index.html",(err,data)=>{
            res.writeHead(200,{
                "Content-type":"application/octet-stream",
                "Content-Disposition":"attachment; filename=index.excell"
            })
            res.end(data)
        })
    }
}).listen(8000)

你可能感兴趣的:(Node.js 实现下载)