路由中间件在Node.js中未正常工作。

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

Router middleware not working properly in Nodejs

问题

我正在使用Node.js并使用"Express.js",目前我正在使用以下代码工作在"路由中间件"功能。每当我访问"http://localhost:3000"时,"路由中间件"会被执行,但我希望只有当我访问"http://localhost:3000/api/"(api路由)时,"路由中间件"才会被激活/执行。以下是我的当前代码:

  1. const express = require('express');
  2. const app = express();
  3. // 创建路由实例
  4. const router = express.Router();
  5. // 定义特定于路由的中间件函数
  6. const routerMiddleware = (req, res, next) => {
  7. // 在请求或响应上执行一些操作
  8. console.log('路由中间件已执行');
  9. next();
  10. };
  11. // 将中间件应用于路由
  12. router.use(routerMiddleware);
  13. // 在路由上定义一个路由
  14. router.get('/example', (req, res) => {
  15. res.send('来自路由的Hello');
  16. });
  17. // 挂载路由到应用
  18. app.use('/api', router);
  19. // 启动服务器
  20. app.listen(3000, () => {
  21. console.log('服务器已启动,端口3000');
  22. });

这个代码会在访问"http://localhost:3000/api/"时激活路由中间件,而在访问"http://localhost:3000"时则不会。

英文:

I am working on Nodejs and using "Express js", Right now i am working on "Router Middleware" function With following code,whenever i hit "http://localhost:3000" then "router middle" working/executed, but i want whenever i hit "http://localhost:3000/api/" (api route) only then "router middleware" should active/execute.Here is my current code

  1. const express = require('express');
  2. const app = express();
  3. // Create a router instance
  4. const router = express.Router();
  5. // Define a middleware function specific to the router
  6. const routerMiddleware = (req, res, next) => {
  7. // Perform some operations on the request or response
  8. console.log('Router middleware executed');
  9. next();
  10. };
  11. // Apply the middleware to the router
  12. router.use(routerMiddleware);
  13. // Define a route on the router
  14. router.get('/example', (req, res) => {
  15. res.send('Hello from the router');
  16. });
  17. // Mount the router on the app
  18. app.use('/api', router);
  19. // Start the server
  20. app.listen(3000, () => {
  21. console.log('Server started on port 3000');
  22. });

答案1

得分: 3

你可以直接将中间件添加到路由器上,而不是使用 router.use(routerMiddleware),像这样:app.use('/api', routerMiddleware, router);

英文:

Instead of using router.use(routerMiddleware) you can add middleware directly to router like this: app.use('/api',routerMiddleware,router);

答案2

得分: 0

中间件的应用顺序很重要,如果您不将中间件应用于某些路由,请在应用中间件之前定义这些路由。

在下面的示例中,/api/example 路由不会触发中间件,因为它在应用中间件之前定义。但是 /api 会触发中间件。

  1. // 在路由上定义一个路由
  2. router.get('/example', (req, res) => {
  3. res.send('我不会触发中间件');
  4. });
  5. // 将中间件应用于路由
  6. router.use(routerMiddleware);
  7. router.get('/', (req, res) => {
  8. res.send('我会触发中间件');
  9. });
  10. // 在应用上挂载路由
  11. app.use('/api', router);

请注意,这是代码示例的中文翻译。

英文:

The order where you apply middleware matters, if you don't apply to middleware to some routes, define that routes before applying the middleware.

In the following example, the /api/example route will not hit the middleware since it is before applying the middleware. But /api will hit.

  1. // Define a route on the router
  2. router.get('/example', (req, res) => {
  3. res.send('I will NOThit middleware');
  4. });
  5. // Apply the middleware to the router
  6. router.use(routerMiddleware);
  7. router.get('/', (req, res) => {
  8. res.send('I will hit the middleware');
  9. });
  10. // Mount the router on the app
  11. app.use('/api', router);

huangapple
  • 本文由 发表于 2023年6月1日 01:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376169.html
匿名

发表评论

匿名网友

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

确定