node.js - 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
谢谢你的帮助!
解决方案
错误在于"proxy": "http://localhost:5003"
最后需要有一个斜线才能使其工作:
"proxy": "http://localhost:5003/"
推荐阅读
- flutter - 创建我的第一个颤振应用程序时遇到问题
- angular - 代码中的角度延迟加载模块(无路由)
- c - 就地向量“abs”警告:操作可能未定义?
- sql-server - 从 SQL Server 导入时,SSIS 包在 VS 中有效,但从文件系统导入时失败(错误 SSIS.Replacement 任务未注册使用)
- php - Laravel 不会通过移动勇敢浏览器将变量保存到数据库
- algorithm - 不知道如何根据值对相邻对象进行分组
- python - 如何在 Django Rest 框架中进行条件渲染?
- node.js - Axios Gzip - 415 不支持的媒体类型和 454 未知
- c++ - 如何在 c++ 中使用在没有 Visual Studio 的 Visual Studio 中创建的 DLL
- python - 有没有办法在 discord.py 中接收其他用户对我的建议