首页 > 解决方案 > API 路由在生产中工作,但不再在 localhost (Node.js) 上工作

问题描述

通常这个问题会反过来,但是对我来说,这些路线在我的本地环境中不再起作用。我不知道问题出在哪里。我没有 .htaccess 文件。这是我的 server.js 文件:

const express = require("express");
const connectDB = require("./config/db");
const enforce = require("express-sslify");
const path = require("path");

const app = express();

//app.use(express.static("public"));

connectDB();

//Inint middleware
//app.use(express.json({ extended: false }));
app.use(express.json());

//Define routes
app.use("/api/users", require("./routes/api/users"));
app.use("/api/auth", require("./routes/api/auth"));
app.use("/api/braintree", require("./routes/api/braintree"));
app.use("/api/meetings", require("./routes/api/meetings"));
app.use("/api/emails", require("./routes/api/emails"));
app.use("/api/features", require("./routes/api/features"));

if (process.env.NODE_ENV === "production") {
  app.use(enforce.HTTPS({ trustProtoHeader: true }));
  //set static folder
  app.use(express.static("client/build"));
  // app.get("*", (req, res) => {
  //   res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  // });
  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

const PORT = process.env.PORT || 5003;

app.listen(PORT, () => console.log("Server started"));

通过 Insomnia/Postman 调用 API 可与 localhost:5003/api/auth/...

我在事先工作的客户端的 package.json 中使用“代理”:“http://localhost:5003”。

在浏览器中,当我调用登录操作时,例如我得到:

POST http://localhost:3000/api/auth 404 (Not Found)
dispatchXhrRequest @ xhr.js:178
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:52
Promise.then (async)
request @ Axios.js:61
Axios.<computed> @ Axios.js:86
wrap @ bind.js:9
(anonymous) @ auth.js:95
(anonymous) @ index.js:8
dispatch @ VM449:1
(anonymous) @ redux.js:477
onSubmit @ LoginNew.js:24
onSubmit @ LoginNew.js:47
callCallback @ react-dom.development.js:188
invokeGuardedCallbackDev @ react-dom.development.js:237
invokeGuardedCallback @ react-dom.development.js:292
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:306
executeDispatch @ react-dom.development.js:389
executeDispatchesInOrder @ react-dom.development.js:414
executeDispatchesAndRelease @ react-dom.development.js:3278
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:3287
forEachAccumulated @ react-dom.development.js:3259
runEventsInBatch @ react-dom.development.js:3304
runExtractedPluginEventsInBatch @ react-dom.development.js:3514
handleTopLevel @ react-dom.development.js:3558
batchedEventUpdates$1 @ react-dom.development.js:21871
batchedEventUpdates @ react-dom.development.js:795
dispatchEventForLegacyPluginEventSystem @ react-dom.development.js:3568
attemptToDispatchEvent @ react-dom.development.js:4267
dispatchEvent @ react-dom.development.js:4189
unstable_runWithPriority @ scheduler.development.js:653
runWithPriority$1 @ react-dom.development.js:11039
discreteUpdates$1 @ react-dom.development.js:21887
discreteUpdates @ react-dom.development.js:806
dispatchDiscreteEvent @ react-dom.development.js:4168

谢谢你的帮助!

标签: node.jsserver

解决方案


错误在于"proxy": "http://localhost:5003"

最后需要有一个斜线才能使其工作:

"proxy": "http://localhost:5003/"

推荐阅读