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.
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
=> Could you tryhttp-server -P http://localhost:8080/
? – Center