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

前端 未结 30 1842
攒了一身酷
攒了一身酷 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:13

    For the benefit of searchers, I liked Jakub g's answer, but wanted a little error handling. Obviously it's best to handle errors properly, but this should help prevent a site stopping if an error occurs. Code below:

    var http = require('http');
    var express = require('express');
    
    process.on('uncaughtException', function(err) {
      console.log(err);
    });
    
    var server = express();
    
    server.use(express.static(__dirname));
    
    var port = 10001;
    server.listen(port, function() { 
        console.log('listening on port ' + port);     
        //var err = new Error('This error won't break the application...')
        //throw err
    });
    

提交回复
热议问题