简体   繁体   English

RectJS 和 NodeJS:从客户端对 url 进行编码和解码以提供服务

[英]RectJS & NodeJS : Encode & decode url from client to serve

 const quParams = {sample1: 'one', sample2: 'one+one/two', sample3: 'testing'} if (Object.keys(quParams).length) { const api_params = `&${Object.keys(quParams).map(x => `${x}=${quParams[x]}`).join("&")}` console.log('api_params>>>>>>>>>>', api_params) }

` `

 const quParam = {sample1: 'one', sample2: 'one%2Bone%2Ftwo'} let objKeys = Object.keys(quParam); let quParamValue = '' for (item of objKeys) { quParam[item]; if (item === 'sample2') { quParamValue = quParam[item].replaceAll('%2F', '/').replaceAll('%2B','+') console.log('quParamValue>>>>>>', quParamValue) } }

`one of the query params of client side url has Special Character in the param(sample2) `客户端url的查询参数之一在参数中有特殊字符(sample2)

https://example.com/endpointService?sample1=one&sample2=one+one/two https://example.com/endpointService?sample1=one&sample2=one+one/two

When passing this url from client side to server side it is encoded as当将此 url 从客户端传递到服务器端时,它被编码为

https://testing.com/endpointService?sample1=one&sample2=one%2Bone%2Ftwo https://testing.com/endpointService?sample1=one&sample2=one%2Bone%2Ftwo

When passing this encodedURL to RESTFUL api service, is not giving good response.将此编码 URL 传递给 RESTFUL api 服务时,没有给出良好的响应。

How to handle this scenario..如何处理这种情况..

Where and how we have to encode & decode the query value using JS我们必须在哪里以及如何使用 JS 对查询值进行编码和解码

Client (React JS) >>> Server (Express JS) >>> Restful Service客户端 (React JS) >>> 服务器 (Express JS) >>> Restful 服务

How to do it without replacing on node js如何在不替换节点js的情况下做到这一点

Most libraries handle this for you so by the time you are sending/getting the request object the params are already encoded/decoded respectively.大多数库会为您处理此问题,因此当您发送/获取请求 object 时,参数已经分别编码/解码。 In case you have to do that by yourself you can use the decodeURI function:如果您必须自己执行此操作,您可以使用decodeURI function:

const uri = 'https://mozilla.org/?x=1+ a/ 2';
const encoded = encodeURI(uri);
console.log(encoded);
// expected output: "https://mozilla.org/?x=1+%20a/%202"

try {
  console.log(decodeURI(encoded));
  // expected output: "https://mozilla.org/?x=1+ a/ 2"
} catch (e) { // catches a malformed URI
  console.error(e);
}

check out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI签出: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI

In express, you can retrieve the url and query params in the route handler:在 express 中,您可以检索 url 并在路由处理程序中查询参数:

app.get('/api/users/:name', function(req, res) {
  const queryParam = req.query.name // for example
  res.send('Hello ' + req.name + '!');
});

If you are not satisfied and need to get params in a special pattern you may create a custom middleware and parse the query string the way you wish.如果您不满意并且需要以特殊模式获取参数,您可以创建自定义中间件并按照您希望的方式解析查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM