无法获取错误消息,使用Nodejs中的Rest API。

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月31日 20:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定