如何在 Node.js 中对某些内容进行 URl 编码?

2022-08-29 23:25:37

我想对这个进行URL编码:

SELECT name FROM user WHERE uid = me() 

我必须为此下载一个模块吗?我已经有请求模块。


答案 1

您可以使用JavaScript的encodeURIComponent

encodeURIComponent('select * from table where i()')

'select%20*%20from%20table%20where%20i()'

答案 2

内置模块查询字符串就是您要查找的内容:

var querystring = require("querystring");
var result = querystring.stringify({query: "SELECT name FROM user WHERE uid = me()"});
console.log(result);
#prints 'query=SELECT%20name%20FROM%20user%20WHERE%20uid%20%3D%20me()'