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

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

Router middleware not working properly in Nodejs

问题

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

const express = require('express');
const app = express();

// 创建路由实例
const router = express.Router();

// 定义特定于路由的中间件函数
const routerMiddleware = (req, res, next) => {
  // 在请求或响应上执行一些操作
  console.log('路由中间件已执行');
  next();
};

// 将中间件应用于路由
router.use(routerMiddleware);

// 在路由上定义一个路由
router.get('/example', (req, res) => {
  res.send('来自路由的Hello');
});

// 挂载路由到应用
app.use('/api', router);

// 启动服务器
app.listen(3000, () => {
  console.log('服务器已启动,端口3000');
});

这个代码会在访问"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

const express = require('express');
const app = express();

// Create a router instance
const router = express.Router();

// Define a middleware function specific to the router
const routerMiddleware = (req, res, next) => {
  // Perform some operations on the request or response
  console.log('Router middleware executed');
  next();
};

// Apply the middleware to the router
router.use(routerMiddleware);

// Define a route on the router
router.get('/example', (req, res) => {
  res.send('Hello from the router');
});

// Mount the router on the app
app.use('/api', router);

// Start the server
app.listen(3000, () => {
  console.log('Server started on port 3000');
});

答案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 会触发中间件。

// 在路由上定义一个路由
router.get('/example', (req, res) => {
    res.send('我不会触发中间件');
});

// 将中间件应用于路由
router.use(routerMiddleware);

router.get('/', (req, res) => {
    res.send('我会触发中间件');
});

// 在应用上挂载路由
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.

// Define a route on the router
router.get('/example', (req, res) => {
    res.send('I will NOThit middleware');
});

// Apply the middleware to the router
router.use(routerMiddleware);

router.get('/', (req, res) => {
    res.send('I will hit the middleware');
});

// Mount the router on the app
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:

确定