我正在构建一个NodeJS / express应用程序,我需要将一些请求转发给我的Heroku API。
我已经探索了许多不同的解决方案(包括this SO question),但它们似乎都不适用于我。
代码:
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创建httpProxy.createProxyServer()
(如上例所示,使用自签名SSL证书)并且没有SSL,但我收到了同样的错误。
答案 0 :(得分:0)
经过大量的试验,错误和拔毛后,我最终在http-proxy-middleware项目上创建了一个问题。
我不知道具体解决了上面提到的错误的原因,但我看到的最大问题是由于http-proxy-middleware
(在引擎盖下使用http-proxy
)而导致其他{{} 1}}中间件位 - 特别是express
。
故事的寓意是body-parser
应该是您配置的第一个(如果不是第一个)中间件之一。