Node.js quick file server (static files over HTTP)

前端 未结 30 2226
攒了一身酷
攒了一身酷 2020-11-22 12:30

Is there Node.js ready-to-use tool (installed with npm), that would help me expose folder content as file server over HTTP.

Example, if I have



        
30条回答
  •  独厮守ぢ
    2020-11-22 13:07

    DEMO/PROTO SERVER ONLY

    If that's all you need, try this:

    const http = require('http');
    const fs = require('fs');
    const port = 3000;
    const app = http.createServer((req,res) => {
        res.writeHead(200);
        if (req.url === '/') req.url = '/index.html'; // courtesy of @JosephCho
        res.end(fs.readFileSync(__dirname + req.url));
    });
    
    app.listen(port);
    

    note: You need to use "/index.html" as part of your address ie "http://localhost:3000/index.html"

提交回复
热议问题