node http-server to respond with index.html to any request

后端 未结 6 1595
迷失自我
迷失自我 2020-12-09 10:31

I have installed http-server globally.

I launch it from myDir on localhost port 8080. In myDir I have index.html

相关标签:
6条回答
  • 2020-12-09 10:54

    To achieve what you are asking for, I recommend you live-server instead of http-server.

    live-server --port=8080 --entry-file=./index.html
    

    live-server is also providing a hot-reload but it was not one of your request

    Edit: live-server is not designed to be used in production. No gzip compression for example

    Edit 2: The maintainer of http-server clearly said in this comment that never, http-server will consider the SPA use-cases

    Edit 3: serve seems to be a good option too

    0 讨论(0)
  • 2020-12-09 11:05

    A bit after the war, but anyway. for angular app, I suggest to add to your package.json:

    "serve-prod": "cp dist/app-name/index.html dist/app-name/404.html && http-server dist/app-name"
    

    Then call

    npm run serve-prod
    
    0 讨论(0)
  • 2020-12-09 11:07

    Simple and straight-forward example using Express 4.x:

    var express = require('express');
    var app = express();
    
    var path = __dirname + '/public';
    var port = 8080;
    
    app.use(express.static(path));
    app.get('*', function(req, res) {
        res.sendFile(path + '/index.html');
    });
    app.listen(port);
    

    This implementation will always respond with index.html if the requested file is not found, and it's almost as simple as using http-server, which lacks this option.

    0 讨论(0)
  • 2020-12-09 11:14

    Use as specified in the documentation.

    http-server --port 8080 -P http://localhost:8080?
    

    Note the ? at the end of the proxy URL.

    0 讨论(0)
  • 2020-12-09 11:19

    Yes there is, with the -P/--proxy option:

    http-server -P http://localhost:8080/
    

    Note that any error, 404 included, will redirect to your index, not just missing path.

    0 讨论(0)
  • 2020-12-09 11:19

    Sometimes for specific cases like this one, it's easy enough to write your own server:

    'use strict';
    var host = '127.0.0.1', port = 3333;
    var path = require('path');
    var app = require('express')();
    app.get('*', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
    app.listen(port, () => console.log(`Listening on http://${host}:${port}/`));
    

    But keep in mind that if every path returns index.html then in the index.html you cannot reference anything like images, style sheets or client side JavaScript files. Not only with the code shown above but with any solution that sends the same response (index.html) to every request.

    You may need to make some exceptions and it's not hard with Express:

    'use strict';
    var host = '127.0.0.1', port = 3333;
    var path = require('path');
    var app = require('express')();
    app.get('/x.png', (req, res) => res.sendFile(path.join(__dirname, 'x.png')));
    app.get('*', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
    app.listen(port, () => console.log(`Listening on http://${host}:${port}/`));
    

    Just keep in mind that the exceptions have to go to the top because the first matching route will be used for a given request.

    Of course you need to save this code e.g. to app.js, install Express:

    npm install express
    

    and start it with:

    node app.js
    

    It's more complicated than using a ready solution (though, as you can see, not that complicated either) but you have much more flexibility in how exactly you want it to behave. It's also easy to add logging etc.

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