在localhost上運行appA:8000-> server是python(Django)並且總是崩潰 我有一個運行在localhost上的expressJs服務器:8080 我是appA的expressJs服務器的靜態文件,它效果很好。expressJs反向代理不工作
問題:我想在expressJs 中創建一個反向代理,以便我可以將任何傳入的api請求轉發到快速服務器。 基本上,應用程序應該與新的expressJs服務器(就像它用來與Django的/ Python的服務器)工作
這裏是我的。但它不起作用。當嘗試點擊localhost:8000 form localhost:8080(例如localhost:8080/api/my-profile)中的端點時,出現錯誤。
forward.js:
var request = require('request')
module.exports = function(pattern, host){
return function(req, res, next){
if(req.url.match(pattern)){
var db_path = req.url.match(pattern)[1]
, db_url = [host, db_path].join('/');
req.pipe(request[req.method.toLowerCase()](db_url)).pipe(res);
}else{
next();
}
}
}
app.js:
var forward = require('./path/to/forward.js');
// instantiate `app` et al
app.use(forward(/\/api\/(.*)/, TARGET_URL));
app.listen(8080);
正則表達式捕獲開始的任何請求/ API /,並捕獲任何以下前綴,所以我們可以適當轉發。