【问题标题】:How to set set url parameter with special character in node js?如何在节点js中设置带有特殊字符的设置url参数?
【发布时间】:2017-03-22 20:35:58
【问题描述】:

我想要这样的设置

localhost:3000/some-special-days-one--parameterThat

如何在node js express框架中设置和获取像这样的url和参数的路由器?

【问题讨论】:

  • 您在几分钟前发布了同样的问题:stackoverflow.com/questions/42960579/…
  • @ZoeCarver 我已经删除了那个问题?对不起
  • 不要一遍又一遍地问同一个问题!如果您之前的问题被删除,可能是有原因的。
  • 你试过什么代码,你在哪里卡住了? - 不是 URL 中的特殊字符。它只是一个普通的 URL 字符。在 URI 末尾指定参数的适当方法是作为查询参数 http://locahost/somePath?someParm=somevalue
  • @Zoecarver 你对吗?对不起。

标签: javascript node.js express


【解决方案1】:

我认为这就是你想要做的:

var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('hello world');
});

app.get('/firstParameter/:id', function (req, res) {
  res.send('my id is: ' + req.params.id);//this is the id
}); 

app.listen(3000);

在本例中,如果用户访问 URL localhost:3000/firstParameter/someId,页面将显示“my id is: someId”。

希望这会有所帮助!

PS

如果您不想拥有/,您可以这样做:

app.get('/:id', function (req, res) {
      var id = req.params.id;
      idArr = id.split("firstParameter--");
      id = idArr[1];

      res.send('my id is: ' + id); //this is the id
}); 

然后,像上面一样,只需转到http://localhost:3000/firstParameter--hello,它就会打印出“我的ID是:你好”

【讨论】:

  • 你太棒了 :) 谢谢 :)
  • 没问题!乐于助人:)
  • 那么@ZoeCarver 我该怎么做:id 参数没有“/”字符?
  • 对不起,我的意思是希望这会有所帮助:|
猜你喜欢
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多