英文:
Cannot GET error message showing using Rest Api in Nodejs
问题
以下是翻译好的部分:
我正在使用Node.js并使用Express.js框架,现在我正在尝试运行"Rest API",但我遇到了以下错误
无法获取/published
这是我的路由文件
module.exports = app => {
const tutorials = require("../controllers/tutorial.controller.js");
var router = require("express").Router();
router.get("/published", tutorials.findAllPublished);
};
这是我的控制器
exports.findAllPublished = (req, res) => {
res.send('Hello world');
};
英文:
I am working with nodejs and using expressjs framework,Right now i am trying to run "Rest Api"
but i am getting following error
Cannot GET /published
Here is my routes file
module.exports = app => {
const tutorials = require("../controllers/tutorial.controller.js");
var router = require("express").Router();
router.get("/published", tutorials.findAllPublished);
};
Here is my controller
exports.findAllPublished = (req, res) => {
res.send('Hello world');
};
答案1
得分: 1
你需要在你的应用程序中的某处使用路由器:app.use('/', router)
。
这部分代码应该可以工作:
module.exports = app => {
const tutorials = require("../controllers/tutorial.controller.js")
var router = require("express").Router()
router.get("/published", tutorials.findAllPublished)
app.use('/', router) // 例如这里
}
英文:
You need to use the router somewhere in your app: app.use('/', router)
This:
module.exports = app => {
const tutorials = require("../controllers/tutorial.controller.js")
var router = require("express").Router()
router.get("/published", tutorials.findAllPublished)
app.use('/', router) // e.g here
}
Should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论