const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
const products_routes = require("./routes/products");
app.get("/", (req, res) => {
res.send("Hello this is my API");
})
app.use("/api/products", products_routes);
const start = async() => {
try {
app.listen(PORT, () => {
console.log(`${PORT} Yes I am connected`);
});
} catch (error) {
console.log(error);
}
}
start();
这是app.js文件
const express = require("express");
const router = express.Router();
const {
getAllProducts,
getAllProductsTesting
} = require("../controllers/products");
router.route("/").get(getAllProducts);
router.route("/testing").get(getAllProductsTesting);
这是route.js文件
const getAllProducts = async (req, res) => {
res.status(200).json({ msg: "I am getAllProducts" });
}
const getAllProductsTesting = async (req, res) => {
res.status(200).json({ msg: "I am getAllProductsTesting" });
}
module.exports = {
getAllProducts,
getAllProductsTesting
};
这是controller.js文件
我期望运行此代码会给我响应“https://localhost:3000/api/products 但它给我找不到 get 错误,现在它给我 nodemon 错误
您在哪一行从
router
文件导出 route.js
?我在您提供的代码中没有看到任何导出语句,而您正在将其导入到 app.js
文件中。