apiに対してwebappを開発しました。 apiがローカルシステム上で動作していないので、私はクロスドメインの問題で動作しないように要求をプロキシする必要があります。これを行う簡単な方法はありますか?私のindex.htmlはローカルから送信され、他のすべてのGET、POST、PUT、DELETEリクエストはxyz.net/apiEndPointに送られます。nodejsを使用したプロキシ
編集:
これは、インデックス、JS、CSSファイルを提供が、外部APIへのルート残りをいけないだろう、私の最初のソリューションが、didntの仕事
var express = require('express'),
app = express.createServer(),
httpProxy = require('http-proxy');
app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);
var proxy = new httpProxy.RoutingProxy();
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
proxy.proxyRequest(req, res, {
host: 'http://apiUrl',
port: 80
});
});
です。これは、私が持っているエラーメッセージです:
An error has occurred: {"stack":"Error: ENOTFOUND, Domain name not found\n at IOWatcher.callback (dns.js:74:15)","message":"ENOTFOUND, Domain name not found","errno":4,"code":"ENOTFOUND"}
ホストは「http://」で始まります。これを削除すると修正されます。 HTTP/1.1 400 Bad Request X-Powered-By:Express サーバー:Apache-Coyote/1.1 コンテンツの長さ:0 date:Sun、25 Sep 2011 18:06: 22 GMT 接続:閉じる –
@AndreasKöberle、まず、この質問を編集するか、新しい質問を開くことをおすすめします。 –
私は新しい質問をしました。 http://stackoverflow.com/questions/7559862/proxy-with-nodejs-and-express –