*代理中的app.use和app.get之间的区别* [英] Difference between app.use and app.get *in proxying*

查看:265
本文介绍了*代理中的app.use和app.get之间的区别*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能更好地理解express的app.get()app.use()之间的区别.

I'm hoping to better understand the difference between express's app.get() and app.use().

我了解app.use适用于所有HTTP动词.

I understand that app.use applies to all HTTP verbs.

我还读到" app.use()添加中间件而不是路由"

I have also read that "app.use() adds middleware rather than a route"

我想了解为什么这个事实会导致这种行为...

I'd like to understand why this fact causes this behaviour...

我有一个Express API服务器,需要代理React开发Web服务器.

I have an express API server that needs to proxy a React development web server.

这意味着必须代理所有非API路由.

This means that all routes that are not API routes have to be proxied.

当我像这样代理路由时,它会起作用:

When I proxy the routes like this, it works:

var proxy = require('express-http-proxy');

module.exports = function set_react_catchall_routes(app) { 
    /* Final route to send anything else to react server. */
    app.get('*', proxy('localhost:3000'));
    app.post('*', proxy('localhost:3000'));
}

但是当我这样做时,它不起作用:

But when I do this it does not work:

    app.use('*', proxy('localhost:3000'));

具体来说,索引"页面已被代理并投放,其内容如下:

Specifically, the "index" page is proxied and served up, with content like this:

 <body>
    <div id="root"></div>
    <script type="text/javascript" src="/static/js/bundle.js"></script>
 </body>

并且客户端请求javascript react bundle,但是随后什么也没有发生".

and the client requests the javascript react bundle, but then "nothing happens".

我有理由确定,当它工作时("GET"和"POST"除外),不会涉及其他" HTTP请求,因为没有记录.

I'm reasonably sure that there aren't "other" HTTP requests involved when it does work (other than GET and POST) because none are logged.

那有什么区别?

推荐答案

尝试将此日志记录放在顶部,它应该有助于弄清正在发生的事情:

Try putting this logging at the top, it should help to clarify what's going on:

app.use(function(req, res, next) {
    // req.path will be '/static/js/bundle.js'
    console.log('path1: ' + req.path);

    next();
});

app.use('*', function(req, res, next) {
    // req.path will be '/'
    console.log('path2: ' + req.path);

    next();
});

app.all('*', function(req, res, next) {
    // req.path will be '/static/js/bundle.js'
    console.log('path3: ' + req.path);

    next();
});

使用app.use时,它将删除req.path的匹配部分.如果您未指定路径(记录第1节),则不会删除任何内容.类似地,第3节使用的是app.all(app.get等都以相同的方式工作),也不会更改req.path.就是第2节.

When you use app.use it will strip off the matching section of req.path. If you don't specify a path (logging section 1) it won't strip anything off. Similarly section 3 is using app.all (app.get, etc. all work the same way) which doesn't change req.path either. It's section 2 that's the kicker.

要了解为什么会发生这种情况,请考虑以下示例:

To understand why this happens consider this example:

var router = express.Router();

router.get('/profile', ...);

app.use('/user', router);

当请求/user/profile时,app.use将删除路径的/user部分.就router而言,路径仅为/profile.

When a request comes in for /user/profile the app.use will strip off the /user part of the path. As far as router is concerned the path is just /profile.

要引用文档,请 http://expressjs.com/en/4x/api.html#req.path

从中间件调用时,安装点不包含在req.path中.

When called from a middleware, the mount point is not included in req.path.

调用app.use的路径有点像开始于",所有匹配的东西都将被丢弃.对于匹配所有内容的*,它会丢弃所有内容.

The path for a call to app.use is a bit like a 'starts with' and anything that matches is thrown away. For * that matches everything so it throws away everything.

如果快速浏览express-http-proxy的源代码,您会发现它使用req.path来确定代理请求的路径.如果仅使用app.use而不使用路径,则应该可以正常工作.

If you take a quick dive into the source code for express-http-proxy you'll see that it uses req.path to determine the path of the proxy request. If you just use app.use without a path it should work fine.

还有一些其他请求属性与理解app.use类似:

There are some other request properties that are similarly relevant to understanding app.use:

  • req.urlreq.path类似,但包含查询字符串.就像req.path一样,它将与mountpath匹配的部分被app.use删除.请注意,Express Request继承自Node的http.IncomingMessageurl属性,因此Express文档中未明确列出该属性.
  • req.originalUrlreq.url相同,但不会被app.use更改.
  • req.baseUrl用于存储路径已被app.use删除的部分.
  • req.url is similar to req.path but with the query string included. Just like req.path it will have the section matching the mountpath removed by app.use. Note that the Express Request inherits the url property from Node's http.IncomingMessage so it isn't explicitly listed in the Express documentation.
  • req.originalUrl starts off the same as req.url but it will not be changed by app.use.
  • req.baseUrl is used to store the section of the path that has been removed by app.use.

有关req.originalUrl的更多详细信息,请参见文档.所有这三个属性.

See the documentation for req.originalUrl for more details on all three of these properties.

这篇关于*代理中的app.use和app.get之间的区别*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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