Nodejs URI malformed的坑 求解救

Nodejs URI malformed的坑 求解救

本来是decodeURIComponent(escape(…))就能解决一切问题的。但是到了Node.js上貌似会变得很复杂 之前就找到遇到同样问题的人了,不过在Github上讨论的方法貌似用在我的项目上是不行的。用到了 querystring.decode(querystring.escape) ,虽然没有报错,但是却是乱码。

URIError: URI malformed
    at decodeURIComponent (native)
    at charenc_UTF8_bytesToString (E:\SourceBox\AuthSite\models\eLib.js:175:12)
    at Object.dec (E:\SourceBox\AuthSite\models\eLib.js:152:9)
    at E:\SourceBox\AuthSite\routes\index.js:7:33
    at callbacks (E:\SourceBox\AuthSite\node_modules\express\lib\router\index.js:161:37)
    at param (E:\SourceBox\AuthSite\node_modules\express\lib\router\index.js:135:11)
    at pass (E:\SourceBox\AuthSite\node_modules\express\lib\router\index.js:142:5)
    at Router._dispatch (E:\SourceBox\AuthSite\node_modules\express\lib\router\index.js:170:5)
    at Object.router (E:\SourceBox\AuthSite\node_modules\express\lib\router\index.js:33:10)
    at next (E:\SourceBox\AuthSite\node_modules\express\node_modules\connect\lib\proto.js:199:15)

实在不知道怎么去解决。

谢谢大家了~


5 回复

Node.js URI malformed 的坑 求解救

在使用 Node.js 处理 URL 编码时,经常会遇到 URI malformed 错误。这种错误通常发生在尝试解码一个格式不正确的 URI 时。本文将通过一个具体的例子来解释如何避免和解决这个问题。

问题背景

假设我们有一个字符串需要进行解码:

const str = "Hello%20World%21";

如果直接使用 decodeURIComponent 进行解码,可能会出现 URI malformed 错误。例如:

try {
    const decodedStr = decodeURIComponent(str);
    console.log(decodedStr); // Hello World!
} catch (error) {
    console.error(error); // URIError: URI malformed
}

解决方案

为了正确处理这种情况,我们需要确保输入的字符串是经过正确编码的。以下是解决这个问题的一个方法:

  1. 检查输入字符串是否已经编码:可以通过正则表达式来检查字符串中是否存在 % 符号,从而判断它是否已经被编码过。

  2. 逐步解码:如果字符串已经被编码过,我们可以先尝试解码,然后再进行一次解码以确保所有字符都被正确解码。

以下是具体的实现代码:

function safeDecodeURIComponent(str) {
    try {
        return decodeURIComponent(str);
    } catch (error) {
        if (error instanceof URIError) {
            // 如果字符串已经被编码过,尝试逐步解码
            let decodedStr = str;
            while (decodedStr.includes('%')) {
                decodedStr = decodeURIComponent(decodedStr);
            }
            return decodedStr;
        } else {
            throw error; // 抛出其他类型的错误
        }
    }
}

// 示例
const str = "Hello%20World%21";
const decodedStr = safeDecodeURIComponent(str);
console.log(decodedStr); // Hello World!

解释

  • safeDecodeURIComponent 函数:该函数首先尝试直接解码字符串。如果发生 URIError,则表示字符串可能已经被编码过。此时,我们通过一个循环逐步解码字符串,直到所有 % 都被替换为止。

  • 逐步解码:通过循环逐步解码,可以确保即使字符串中有多个 % 编码的字符,也能被正确解码。

通过这种方法,可以有效避免 URI malformed 错误,并且能够正确解码字符串。希望这个解决方案能帮助你解决类似的问题!


没人知道?

try catch

URI malformed 错误通常是因为尝试解码一个无效的URI字符串。在Node.js中,decodeURIComponent 方法用于解码使用 encodeURIComponent 编码的URI组件,如果传入的参数不是一个有效的编码URI,则会抛出这个错误。

根据你的描述,你在使用 decodeURIComponent 时遇到了问题。这里有几个可能的原因和解决方案:

  1. 确保输入有效:确保你传递给 decodeURIComponent 的字符串是经过 encodeURIComponent 正确编码的。

  2. 检查字符集:有时候URI中的某些特殊字符可能不被正确处理。确保你的字符集设置正确。

  3. 使用 querystring 模块:如果你在处理查询字符串,可以考虑使用Node.js内置的 querystring 模块。

示例代码

假设你有一个从URL中获取的编码字符串,你可以这样解码它:

const querystring = require('querystring');

// 假设这是从URL中获取的编码字符串
const encodedURIComponent = "Hello%20World%21"; // 这里%20表示空格

try {
    const decodedURIComponent = decodeURIComponent(encodedURIComponent);
    console.log(decodedURIComponent); // 输出: Hello World!
} catch (error) {
    console.error("解码失败:", error.message);
}

如果你需要处理的是查询字符串(例如,name=John+Doe),可以这样做:

const querystring = require('querystring');

// 假设这是从URL中获取的查询字符串
const queryString = 'name=John%20Doe';

try {
    const parsedQuery = querystring.parse(queryString);
    console.log(parsedQuery); // 输出: { name: 'John Doe' }
} catch (error) {
    console.error("解析查询字符串失败:", error.message);
}

通过上述方法,你应该能够避免 URI malformed 错误,并正确解码URI或查询字符串。

回到顶部