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

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

    I know it's not Node, but I've used Python's SimpleHTTPServer:

    python -m SimpleHTTPServer [port]
    

    It works well and comes with Python.

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

    Below worked for me:

    Create a file app.js with below contents:

    // app.js
    
    var fs = require('fs'),
        http = require('http');
    
    http.createServer(function (req, res) {
      fs.readFile(__dirname + req.url, function (err,data) {
        if (err) {
          res.writeHead(404);
          res.end(JSON.stringify(err));
          return;
        }
        res.writeHead(200);
        res.end(data);
      });
    }).listen(8080);
    

    Create a file index.html with below contents:

    Hi
    

    Start a command line:

    cmd
    

    Run below in cmd:

    node app.js
    

    Goto below URL, in chrome:

    http://localhost:8080/index.html
    

    That's all. Hope that helps.

    Source: https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

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

    I haven't had much luck with any of the answers on this page, however, below seemed to do the trick.

    Add a server.js file with the following content:

    const express = require('express')
    const path = require('path')
    const port = process.env.PORT || 3000
    const app = express()
    
    // serve static assets normally
    app.use(express.static(__dirname + '/dist'))
    
    // handle every other route with index.html, which will contain
    // a script tag to your application's JavaScript file(s).
    app.get('*', function (request, response){
      response.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
    })
    
    app.listen(port)
    console.log("server started on port " + port)
    

    Also make sure that you require express. Run yarn add express --save or npm install express --save depending on your setup (I can recommend yarn it's pretty fast).

    You may change dist to whatever folder you are serving your content is. For my simple project, I wasn't serving from any folder, so I simply removed the dist filename.

    Then you may run node server.js. As I had to upload my project to a Heroku server, I needed to add the following to my package.json file:

      "scripts": {
        "start": "node server.js"
      }
    
    0 讨论(0)
  • 2020-11-22 13:06

    If you use the Express framework, this functionality comes ready to go.

    To setup a simple file serving app just do this:

    mkdir yourapp
    cd yourapp
    npm install express
    node_modules/express/bin/express
    
    0 讨论(0)
  • 2020-11-22 13:06

    First install node-static server via npm install node-static -g -g is to install it global on your system, then navigate to the directory where your files are located, start the server with static it listens on port 8080, naviaget to the browser and type localhost:8080/yourhtmlfilename.

    0 讨论(0)
  • 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"

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