Node.js url.parse 结果返回字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7517332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:36:23  来源:igfitidea点击:

Node.js url.parse result back to string

urlnode.js

提问by Ben Humphreys

I am trying to do some simple pagination. To that end, I'm trying to parse the current URL, then produce links to the same query, but with incremented and decremented pageparameters.

我正在尝试做一些简单的分页。为此,我试图解析当前的 URL,然后生成指向同一查询的链接,但使用递增和递减的page参数。

I've tried doing the following, but it produces the same link, without the new pageparameter.

我尝试执行以下操作,但它生成相同的链接,但没有新page参数。

var parts = url.parse(req.url, true);
parts.query['page'] = 25;
console.log("Link: ", url.format(parts));

The documentation for the URL moduleseems to suggest that formatis what I need but I'm doing something wrong.

URL 模块文档似乎表明这format是我需要的,但我做错了什么。

I know I could iterate and build up the string manually, but I was hoping there's an existing method for this.

我知道我可以手动迭代和构建字符串,但我希望有一个现有的方法。

回答by Alex Turpin

If you look at the latest documentation, you can see that url.formatbehaves in the following way:

如果您查看最新的文档,您会发现它的url.format行为方式如下:

  • searchwill be used in place of query
  • query(object; see querystring) will only be used if searchis absent.
  • search将用于代替 query
  • query(object; see querystring) 仅在search不存在时使用。

And when you modify query, searchremains unchanged and it uses it. So to force it to use query, simply remove searchfrom the object:

当您修改query, 时search保持不变并使用它。所以要强制它使用query,只需search从对象中删除:

var url = require("url");
var parts = url.parse("http://test.com?page=25&foo=bar", true);
parts.query.page++;
delete parts.search;
console.log(url.format(parts)); //http://test.com/?page=26&foo=bar

Make sure you're always reading the latest version of the documentation, this will save you a lot of trouble.

确保您始终阅读最新版本的文档,这将为您省去很多麻烦。

回答by Marcel M.

Seems to me like it's a bug in node. You might try

在我看来,这是 node.js 中的一个错误。你可以试试

// in requires
var url = require('url');
var qs = require('querystring');

// later
var parts = url.parse(req.url, true);
parts.query['page'] = 25;
parts.query = qs.stringify(parts.query);
console.log("Link: ", url.format(parts));

回答by ampersand

The other answer is good, but you could also do something like this. The querystringmodule is used to work with query strings.

另一个答案很好,但你也可以做这样的事情。该querystring模块用于处理查询字符串。

var querystring = require('querystring');
var qs = querystring.parse(parts.query);
qs.page = 25;
parts.search = '?' + querystring.stringify(qs);
var newUrl = url.format(parts);

回答by d1b1

To dry out code and get at URL variables without needing to require('url') I used:

为了在不需要 require('url') 的情况下干燥代码并获取 URL 变量,我使用了:

/* 
   Used the url module to parse and place the parameters into req.urlparams.
   Follows the same pattern used for swagger API path variables that load 
   into the req.params scope.

*/
  app.use(function(req, res, next) {
    var url = require('url');
    var queryURL = url.parse(req.url, true);
    req.urlparams = queryURL.query;
    next();
  });

var myID = req.urlparams.myID;

This will parse and move the url variables into the req.urlparams variable. It runs early in the request workflow so is available for all expressjs paths.

这将解析 url 变量并将其移动到 req.urlparams 变量中。它在请求工作流的早期运行,因此可用于所有 expressjs 路径。