使用node.js重写URL路径 [英] Rewrite url path using node.js

查看:183
本文介绍了使用node.js重写URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用node.js重写URL路径?(我也使用Express 3.0)

Is it possible to rewrite the URL path using node.js?(I'm also using Express 3.0)

我已经尝试过这样的事情: p>

I've tried something like this:

req.url = 'foo';

但网址继续相同

推荐答案

当然,只需添加一个中间件功能来修改它。例如:

Sure, just add a middleware function to modify it. For example:

app.use(function(req, res, next) {
  if (req.url.slice(-1) === '/') {
    req.url = req.url.slice(0, -1);
  }
  next();
});

此功能从所有传入的请求URL中删除尾部斜线。请注意,为了使其正常工作,您需要将其放在调用 app.use(app.router)之前。

This function removes the trailing slash from all incoming request URLs. Note that in order for this to work, you will need to place it before the call to app.use(app.router).

这篇关于使用node.js重写URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆