express.js routes explanation

前端 未结 1 1074
长发绾君心
长发绾君心 2021-02-15 01:28

I was looking at express.js source code, to find out how it maps named route parameters to req.params properties.

For those who don\'t know, in exp

1条回答
  •  没有蜡笔的小新
    2021-02-15 02:20

    It is for matching file extensions and such properly.

    Given the path '/path/:file.:ext', consider the difference between the expressions:

    // With 'format' checking
    /^\/path\/(?:([^\/]+?))(?:\.([^\/\.]+?))\/?$/
    
    // Without 'format' checking
    /^\/path\/(?:([^\/]+?))(?:([^\/]+?))\/?$/
    

    In the first case, you end up with params as

    {
        file: 'file',
        ext: 'js'
    }
    

    but without the format checking, you end up with this:

    {
        file: 'f',
        ext: 'ile.js'
    }
    

    0 讨论(0)
提交回复
热议问题