Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Nodejs express正则表达式路由,其中包含变量-为什么不匹配?_Node.js_Regex_Express - Fatal编程技术网

Node.js Nodejs express正则表达式路由,其中包含变量-为什么不匹配?

Node.js Nodejs express正则表达式路由,其中包含变量-为什么不匹配?,node.js,regex,express,Node.js,Regex,Express,我使用的是Express4.16,我想创建一个路由,它接受每个路径的末尾都有一个变量 我试过这样的东西,但不匹配:S ... router.get('/.*:name$/', (req, res) => { ... 例如: .../animal/Joe .../fruit/apple.txt .../people/man/Sam 我使用此页面对其进行了测试: 编辑1----------------- 正如我所提到的,我正在尝试创建一个SVGAPI,它以不同的颜色返回

我使用的是Express4.16,我想创建一个路由,它接受每个路径的末尾都有一个变量

我试过这样的东西,但不匹配:S

 ...
    router.get('/.*:name$/', (req, res) => {
    ...
例如:

.../animal/Joe
.../fruit/apple.txt
.../people/man/Sam
我使用此页面对其进行了测试:

编辑1----------------- 正如我所提到的,我正在尝试创建一个SVGAPI,它以不同的颜色返回一个公共svg文件。示例:localhost:3000/svg/logo/logo.svg?color=#2323

server.js:

..
const svg = require('./server/routes/svg');
...
app.use('/svg', svg);
...
svg.js

const express = require('express');
const router  = express.Router();
var   fs      = require('fs');

router.get('/.*:name$/', (req, res) => {
    let name  = req.params.name;
    let color = req.query.color;
    console.log(req.path, color, req.originalUrl, req.path);
    if(typeof name != 'undefined' && typeof color != 'undefined'){
        res.setHeader('Content-Type', 'image/svg+xml');
        //                  here I should concatenate req.path before name or something like this idk..
        let read = fs.readFile(`${__dirname}/../../dist/assets/images/svg/${name}.svg`, 'utf8', (err, template) => {
            if(typeof template !== 'undefined'){
                let svg = template.replace(/\#000000/g, (match) => {
                    return color;
                });
                return res.send(svg);
            }else{
                return res.status(404).send();
            }
        });
    }else{
        return res.status(404).send();
    }
});

module.exports = router;
(此代码不完整,我只是卡住了路由器)


提前感谢您的时间!:)

根据我的判断,你可以用一个未命名的正则表达式来做你想要做的事情

router.get('/svg/path/(.*)', (req, res) => {
  // in here, you'll get the full path with req.path and then parse it validate it's the right form and to get the info you need out of it.

  // You can also use req.query to get the color param you mentioned.  
});

与我更好的判断相反,你可以用一个未命名的正则表达式来做你想要做的事情

router.get('/svg/path/(.*)', (req, res) => {
  // in here, you'll get the full path with req.path and then parse it validate it's the right form and to get the info you need out of it.

  // You can also use req.query to get the color param you mentioned.  
});
玩过之后,我想出了这个模式:

*/:name
根据Route Tester,此模式将被编译(对regexp 0.1.7的路径有效)为以下正则表达式:

/^(.*)\/(?:([^\/]+?))\/?$/i
^(.*)
将从一开始就捕获所有内容

([^\/]+?)
将捕获最后一个值并将其存储到
名称中

注意:需要注意的是,
*
被编译为
(.*)

对于最新版本的path to regexp,以下模式应等效:

(.*)/:name
希望有帮助

玩过之后,我想到了这个模式:

*/:name
根据Route Tester,此模式将被编译(对regexp 0.1.7的路径有效)为以下正则表达式:

/^(.*)\/(?:([^\/]+?))\/?$/i
^(.*)
将从一开始就捕获所有内容

([^\/]+?)
将捕获最后一个值并将其存储到
名称中

注意:需要注意的是,
*
被编译为
(.*)

对于最新版本的path to regexp,以下模式应等效:

(.*)/:name

希望有帮助

您的问题不清楚您正试图匹配什么以及希望捕获什么参数。我想创建一个api,根据localhost:3000/svg/pathWhereicandFindthisfile/icon.svg?color=#23232323等路径返回svg文件,以便读取svg修改其颜色并将其作为响应返回。我只是想缩小我的问题:)如果你想让随机的网络用户像那样遍历你的目录,那就太危险了。你能解释一下为什么吗?如果路径或颜色参数末尾没有svg名称,我将返回一个空svg。另一方面,使用正确的路径、文件名和有效的颜色,我返回一个具有不同颜色的公共文件。我不认为这可能是危险的,但我猜这个页面做了一些非常类似于他的下载SVG(不确定)您的问题不清楚您正试图匹配什么以及希望捕获什么参数。我想创建一个api,根据localhost:3000/svg/pathWhereicandFindthisfile/icon.svg?color=#23232323等路径返回svg文件,以便读取svg修改其颜色并将其作为响应返回。我只是想缩小我的问题:)如果你想让随机的网络用户像那样遍历你的目录,那就太危险了。你能解释一下为什么吗?如果路径或颜色参数末尾没有svg名称,我将返回一个空svg。另一方面,使用正确的路径、文件名和有效的颜色,我返回一个具有不同颜色的公共文件。我不认为这可能是危险的,但是我猜这个页面做了一些非常类似于他的可下载的SVG(不确定)这个路由器讨厌我。仍然不匹配:(这台路由器恨我。仍然不匹配:(我的英雄!非常感谢你和其他人的时间:)我的英雄!非常感谢你和其他人的时间:)