0

我们的 React Development 构建在 localhost 上使用 Express Gateway 设置运行完美。在为生产构建 React 之后,当我们运行 serve -s 构建登录页面时,它是应用程序的入口点。当我们输入登录凭据时,它会得到 200 ok 响应。但是当我们查看它时,我们可以看到对服务器的请求不成功,因为它保存到浏览器应用程序的令牌是未定义的,我们检查了响应,它是“您需要启用 javascript...”。JS 无疑是启用的。我已经检查了通过使用

axios.post('http://localhost:8080/api/v1/auth/sign-in', userData)

它工作正常,但是当设置代理时: axios.post('/auth/sign-in', userData) react doesn't run 这是用于快速网关设置的 yml 部分:

http:
port: 8080
apiEndpoints:
 auth-service:
  host: "*"
  paths: ["/api/v1/auth/*", "/api/v1/auth"]
 mail-service:
  host: "*"
  paths: ["/api/v1/mail/*", "/api/v1/mail"]
serviceEndpoints:
 auth-service-endpoint:
  url: http://localhost:3003/
 mail-service-endpoint:
  url: http://localhost:3005/
policies:
 - proxy
pipelines:
 auth-service-pipeline:
  apiEndpoints:
   - auth-service
  policies:
   - proxy:
      action:
        serviceEndpoint: auth-service-endpoint
        changeOrigin: true
        stripPath: true
  mail-service-pipeline:
   apiEndpoints:
    - mail-service
   policies:
    - proxy:
      action:
        serviceEndpoint: mail-service-endpoint
        changeOrigin: true
        stripPath: true

我将 setupProxy.js 放在 React 的 src 目录中:

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(createProxyMiddleware('/api/v1', 
    { target: 'http://localhost:8080',
     secure: false,
     changeOrigin: true,
    // pathRewrite: {
    //   "^/api": "/api/v1",
    // }
}
));

}

目前一切都在同一台机器上。我们没有使用 docker。该应用程序在开发环境中运行,但在生产构建中显示 200 ok 响应任何帮助将不胜感激。

[编辑]

krypton:admin-dashboard-server hasan$ curl -v http://localhost:3001/find_all_services/1/10
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3001 (#0)
> GET /find_all_services/1/10 HTTP/1.1
> Host: localhost:3001
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< X-DNS-Prefetch-Control: off
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=15552000; includeSubDomains
< X-Download-Options: noopen
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Content-Type: application/json; charset=utf-8
< Content-Length: 1833
< ETag: W/"729-LM91B3vCUrbvesBrp32ykiXXkQo"
< Date: Tue, 12 Jan 2021 14:57:24 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
[{"id":1,"name":"Laser Hair Remove"},
 {"id":2,"name":"Facial Treatments"}
]

krypton:admin-dashboard-server hasan$ curl -v 
http://localhost:8080/api/v1/services/find_all_services/1/10
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /api/v1/services/find_all_services/1/10 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< access-control-allow-origin: *
< x-dns-prefetch-control: off
< x-frame-options: SAMEORIGIN
< strict-transport-security: max-age=15552000; includeSubDomains
< x-download-options: noopen
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< content-type: application/json; charset=utf-8
< content-length: 1833
< etag: W/"729-LM91B3vCUrbvesBrp32ykiXXkQo"
< date: Tue, 12 Jan 2021 15:03:45 GMT
< connection: keep-alive
< 
* Connection #0 to host localhost left intact
[{"id":1,"name":"Laser Hair Remove"},
 {"id":2,"name":"Facial Treatments"}
]


krypton:admin-dashboard-server hasan$ curl -v -H "Content-Type: application/json" -X POST -d 
'{"email":"mh.mithun@gmail.com","password":"safe123"}' 
http://localhost:8080/api/v1/auth/sign-in
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /api/v1/auth/sign-in HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 52
> 
* upload completely sent off: 52 out of 52 bytes
< HTTP/1.1 200 OK
< access-control-allow-origin: *
< x-dns-prefetch-control: off
< x-frame-options: SAMEORIGIN
< strict-transport-security: max-age=15552000; includeSubDomains
< x-download-options: noopen
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< content-type: application/json; charset=utf-8
< content-length: 270
< etag: W/"10e-S+kd8b4Yfl7un04FVGe3MFLFEaY"
< date: Tue, 12 Jan 2021 15:40:12 GMT
< connection: keep-alive
< 
* Connection #0 to host localhost left intact
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGdvcml0aG0iOiJIUzI1N"

krypton:admin-dashboard-server hasan$ curl -v -H "Content-Type: application/json" -X POST -d '{"email":"mh.mithun@gmail.com","password":"safe123"}' http://localhost:3003/sign-in
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3003 (#0)
> POST /sign-in HTTP/1.1
> Host: localhost:3003
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 52
> 
* upload completely sent off: 52 out of 52 bytes
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< X-DNS-Prefetch-Control: off
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=15552000; includeSubDomains
< X-Download-Options: noopen
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Content-Type: application/json; charset=utf-8
< Content-Length: 270
< ETag: W/"10e-LW/1l5fXf5BaiF3KJMvG60xRthE"
< Date: Tue, 12 Jan 2021 15:45:33 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGdvcml0aG0i"
4

0 回答 0