通常这个问题反过来,但是对我来说,路线在我的本地环境中不再起作用。我不知道问题出在哪里。我没有.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中使用“ proxy”:“ 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
感谢您的帮助!
答案 0 :(得分:0)
错误在于"proxy": "http://localhost:5003"
要使其正常工作,必须在末尾加一个斜杠:
"proxy": "http://localhost:5003/"