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

前端 未结 30 2210
攒了一身酷
攒了一身酷 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 12:57

    Install express using npm: https://expressjs.com/en/starter/installing.html

    Create a file named server.js at the same level of your index.html with this content:

    var express = require('express');
    var server = express();
    server.use('/', express.static(__dirname + '/'));
    server.listen(8080);
    

    If you wish to put it in a different location, set the path on the third line:

    server.use('/', express.static(__dirname + '/public'));
    

    CD to the folder containing your file and run node from the console with this command:

    node server.js
    

    Browse to localhost:8080

提交回复
热议问题