我的 express 應用程式在 localhost 上運行良好,但在 Heroku 上無法運行。當我添加一行時,它停止作業并且
這條線是
app.use("/api/product", require("./routes/product"))
這是代碼 Index.js
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.send("responded")
});
app.use(express.json())
app.use("/api/product", require("./routes/product"))
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
產品.js
const express = require("express");
const router = express.Router();
router.get("/", async (req, res) => {
try {
res.json({
status: 200,
message: "Data has been successfully fetched"
});
}
catch (error) {
console.log(error);
return res.status(400).send("server error")
}
})
module.exports = router;
包.json
{
"name": "backend-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.3"
}
}
檔案夾結構
uj5u.com熱心網友回復:
你會想切換你的路線處理程式的地方。否則,您將永遠不會豐富您的 api,因為第一個會捕獲所有請求。
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
app.use(express.json())
app.use("/api/product", require("./routes/product"))
app.get("/", (req, res) => {
res.send("responded")
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440871.html
標籤:javascript 节点.js 表示
