簡體   English   中英

適用於Heroku的NodeJS代理服務器

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM