Skip to content

Instantly share code, notes, and snippets.

@vvscode
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvscode/10834940 to your computer and use it in GitHub Desktop.
Save vvscode/10834940 to your computer and use it in GitHub Desktop.
Lightweight server to run and test SPA bounded to API service
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var dirname = __dirname;
var port = process.env.PORT || 8081;
var extservice = 'xxx.ru';
var extProtocol = 'http:';
var extPort = 9000;
app.use(express.static(dirname));
app.listen(port);
var proxy = new httpProxy.createServer({
target: {
protocol: 'http:'
}
});
app.get('/', function (req, res) {
res.sendfile(dirname + '/index.html');
});
app.get('*.html', function (req, res) {
console.info(dirname + req.url);
res.sendfile(dirname + req.url);
});
app.get('*.js', function (req, res) {
console.info(dirname + req.url);
res.sendfile(dirname + req.url);
});
app.get('*.css', function (req, res) {
console.info(dirname + req.url);
res.sendfile(dirname + req.url);
});
app.all('*', function (req, res) {
console.info(req.url);
proxy.web(req, res, { target: extProtocol + '//' + extservice + ':' + extPort });
});
console.log('Server running on http://localhost:' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment