【发布时间】:2011-09-25 11:54:18
【问题描述】:
我针对 api 开发了一个 webapp。由于 api 没有在我的本地系统上运行,我需要代理请求,所以我不会在跨域问题中运行。有没有一种简单的方法可以做到这一点,所以我的 index.html 将从本地和所有其他 GET、POST、PUT、DELETE 请求发送到 xyz.net/apiEndPoint。
编辑:
这是我的第一个解决方案,但没有奏效
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
});
});
它将提供索引、js、css 文件,但不会将其余文件路由到外部 api。这是我收到的错误消息:
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"}
【问题讨论】: