Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
Javascript 如何在管线参数中使用斜线_Javascript_Node.js_Rest_Url_Express - Fatal编程技术网

Javascript 如何在管线参数中使用斜线

Javascript 如何在管线参数中使用斜线,javascript,node.js,rest,url,express,Javascript,Node.js,Rest,Url,Express,我有一个GET-REST服务,它需要接受带有/ URL=“/term/:term/amount/:amount” 其中:术语可以是类似“para/5MG”的字符串 有没有办法在express中执行此操作?当我的api被使用时,我不想用queryparams重写它。在本机上,express尝试在/处进行拆分,因此您必须手动进行拆分。下面是这样做的一个例子: app.get('/term/:term/amount/:amount', function(req, res) { // your

我有一个GET-REST服务,它需要接受带有/

URL=“/term/:term/amount/:amount” 其中:术语可以是类似“para/5MG”的字符串


有没有办法在express中执行此操作?当我的api被使用时,我不想用queryparams重写它。

在本机上,express尝试在
/
处进行拆分,因此您必须手动进行拆分。下面是这样做的一个例子:

app.get('/term/:term/amount/:amount',  function(req, res) {
    // your code here
})
app.get('/term/\\S+/amount/:amount', function (req, res, next){
  var match;
  if(match = req.path.match(/^\/term\/(.*?)\/amount\/(.*)$/)){
    var term = match[1];
    var amount = req.params.amount;
    // or do whatever you like

    res.json({term: term, amount: amount})
  }else{
    res.sendStatus(404);
  }
})

用这种方法你会失去很多expresse的内置魔法。首先对参数进行URI编码可能会更好。(像这样:
term/para%2F5MG/amount/3

编码斜杠对我不起作用,你试过
curl/term/para/5MG/amount/5
?你需要对正斜杠进行URI编码,试试这个:
curl/term/para%2F5MG/amount/5
那为什么这不是你答案的一部分呢?我在最初阅读这个问题时没有注意到正斜杠作为参数的细微差别。我想OP只是不知道如何创建带有命名参数的快速路由。谢谢,URI编码似乎是最好的方法。