Last active
August 29, 2015 13:59
-
-
Save vvscode/10834940 to your computer and use it in GitHub Desktop.
Lightweight server to run and test SPA bounded to API service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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