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

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

    For dev work you can use (express 4) https://github.com/appsmatics/simple-httpserver.git

    0 讨论(0)
  • 2020-11-22 13:12

    A good "ready-to-use tool" option could be http-server:

    npm install http-server -g
    

    To use it:

    cd D:\Folder
    http-server
    

    Or, like this:

    http-server D:\Folder
    

    Check it out: https://github.com/nodeapps/http-server

    0 讨论(0)
  • 2020-11-22 13:12

    I use Houston at work and for personal projects, it works well for me.

    https://github.com/alejandro/Houston

    0 讨论(0)
  • 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
    });
    
    0 讨论(0)
  • 2020-11-22 13:18

    From npm@5.2.0, npm started installing a new binary alongside the usual npm called npx. So now, one liners to create static http server from current directory:

    npx serve
    

    or

    npx http-server
    
    0 讨论(0)
  • 2020-11-22 13:19

    A simple Static-Server using connect

    var connect = require('connect'),
      directory = __dirname,
      port = 3000;
    
    connect()
      .use(connect.logger('dev'))
      .use(connect.static(directory))
      .listen(port);
    
    console.log('Listening on port ' + port);
    

    See also Using node.js as a simple web server

    0 讨论(0)
提交回复
热议问题