[英]NodeJS Proxy Server for Heroku
我正在构建一个NodeJS / express应用程序,其中需要将一些请求转发到我的Heroku API。
我探索了许多不同的解决方案(包括这个SO问题 ),但是似乎没有一个对我有用。
码:
const API_SERVER = 'https://my-api.herokuapp.com';
const path = require('path'),
fs = require('fs'),
express = require('express'),
router = express.Router(),
httpProxy = require('http-proxy'),
apiProxy = httpProxy.createProxyServer({
target: API_SERVER,
secure: true,
ssl: {
key: fs.readFileSync(path.join('ssl', 'key.pem'),'utf8'),
cert: fs.readFileSync(path.join('ssl', 'cert.pem'), 'utf8')
}
});
router.all('/api/*', (req, res) => {
apiProxy.web(req, res);
});
module.exports = router;
我的快速服务器正常运行,但是当我向localhost/api/some-route
请求时,出现以下错误:
Error: Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not in the cert's altnames: DNS:*.herokuapp.com, DNS:herokuapp.com"
at Object.checkServerIdentity (tls.js:199:17)
at TLSSocket.<anonymous> (_tls_wrap.js:1091:29)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:603:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:433:38)
Details:
killed: false
code: 1
signal: null
cmd: node ./server/index.js
Stack:
Error: Command failed: node ./server/index.js
C:\Users\web-app\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^
Error: Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not in the cert's altnames: DNS:*.herokuapp.com, DNS:herokuapp.com"
at Object.checkServerIdentity (tls.js:199:17)
at TLSSocket.<anonymous> (_tls_wrap.js:1091:29)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:603:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:433:38)
at ChildProcess.exithandler (child_process.js:206:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
从我可以从该错误消息中收集到的信息来看,Heroku似乎不喜欢我从本地主机代理的事实……但是我不确定是否还可以尝试其他方法。
我尝试使用SSL(如上例中使用自签名SSL证书)和没有SSL的方式创建httpProxy.createProxyServer()
,但是却遇到相同的错误。
经过大量的尝试,错误和拖拉操作,我最终在http-proxy-middleware项目上创建了一个问题。
我不知道具体是什么解决了我上面提到的错误,但是我看到的最大问题是由在其他express
中间件(特别是body-parser
)之后配置http-proxy-middleware
(在后台使用http-proxy
)引起的。
这个故事的寓意是, http-proxy-middleware
应该属于您配置的第一个(如果不是第一个)中间件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.