简体   繁体   English

URL 解析与构造函数:缺少路径

[英]URL parse vs constructor: path missing

I am total beginner in Node.js but I am trying to fix what I thought was a simple issue.我是 Node.js 的初学者,但我正在尝试解决我认为是一个简单的问题。 I am using the following code example for an AWS Lambda function using Node.js 12 runtime:我使用 Node.js 12 运行时的 AWS Lambda function 使用以下代码示例:

function respond(event, context, responseStatus, responseData, physicalResourceId, noEcho) {
    var responseBody = JSON.stringify({
        Status: responseStatus,
        Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
        PhysicalResourceId: physicalResourceId || context.logStreamName,
        StackId: event.StackId,
        RequestId: event.RequestId,
        LogicalResourceId: event.LogicalResourceId,
        NoEcho: noEcho || false,
        Data: responseData
    });
    var https = require("https");
    var url = require("url");
    var parsedUrl = url.parse(event.ResponseURL);
    var options = {
        hostname: parsedUrl.hostname,
        port: 443,
        path: parsedUrl.path,
        method: "PUT",
        headers: {
            "content-type": "",
            "content-length": responseBody.length
        }
    };
    var request = https.request(options, function(response) {
        context.done();
    });
    request.on("error", function(error) {
        console.log("send(..) failed executing https.request(..): " + error);
        context.done();
    });
    request.write(responseBody);
    request.end();
}

Full source code can be found here: https://github.com/aws-samples/amazon-cloudfront-secure-static-site/blob/7f96cdbcfbd7f94c3ab5a4c028b6bafd10744c83/source/witch/witch.js#L70完整的源代码可以在这里找到: https://github.com/aws-samples/amazon-cloudfront-secure-static-site/blob/7f96cdbcfbd7f94c3ab5a4c028b6bafd10744c83/source/witch/witch.js#L70

My IDE gives me a warning that the URL.parse() method is deprecated and so that I should use the URL constructor instead.我的 IDE 警告我 URL.parse() 方法已弃用,因此我应该改用 URL 构造函数。 So the only change I made is replacing:所以我做的唯一改变是替换:

var parsedUrl = url.parse(event.ResponseURL);

with

var parsedUrl = new url.URL(event.ResponseURL);

But when I do that, the options.path field ends up missing.但是当我这样做时, options.path 字段最终会丢失。 What is even more confusing to me is that if I log the parsedUrl variable (passing it through JSON.stringify()), I can see that when I use url.parse(), I get a simple string in parsedUrl:更让我困惑的是,如果我记录 parsedUrl 变量(通过 JSON.stringify() 传递它),我可以看到当我使用 url.parse() 时,我在 parsedUrl 中得到一个简单的字符串:

"https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A123456789012%3Astack/AcmCertificateStack-ABCDEFGHIJKL/00112233-4455-6677-8899-aabbccddeeff%7CCopyCustomResource%7C00112233-4455-6677-8899-aabbccddeeff?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220101T000000Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7199&X-Amz-Credential=ABCDEFGHIJKLMNOPQRST%2F20220101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"

But when using the constructor, I can see in the log an object structure with all the expected fields (protocol, hostname, port, even path):但是在使用构造函数时,我可以在日志中看到一个 object 结构,其中包含所有预期的字段(协议、主机名、端口,甚至路径):

{
    "protocol": "https:",
    "slashes": true,
    "auth": null,
    "host": "cloudformation-custom-resource-response-useast1.s3.amazonaws.com",
    "port": null,
    "hostname": "cloudformation-custom-resource-response-useast1.s3.amazonaws.com",
    "hash": null,
    "search": "?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220101T000000Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7199&X-Amz-Credential=ABCDEFGHIJKLMNOPQRST%2F20220101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
    "query": "XX-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220101T000000Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7199&X-Amz-Credential=ABCDEFGHIJKLMNOPQRST%2F20220101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
    "pathname": "/arn%3Aaws%3Acloudformation%3Aus-east-1%3A123456789012%3Astack/AcmCertificateStack-ABCDEFGHIJKL/00112233-4455-6677-8899-aabbccddeeff%7CCopyCustomResource%7C00112233-4455-6677-8899-aabbccddeeff",
    "path": "/arn%3Aaws%3Acloudformation%3Aus-east-1%3A123456789012%3Astack/AcmCertificateStack-ABCDEFGHIJKL/00112233-4455-6677-8899-aabbccddeeff%7CCopyCustomResource%7C00112233-4455-6677-8899-aabbccddeeff?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220101T000000Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7199&X-Amz-Credential=ABCDEFGHIJKLMNOPQRST%2F20220101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
    "href": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A123456789012%3Astack/AcmCertificateStack-ABCDEFGHIJKL/00112233-4455-6677-8899-aabbccddeeff%7CCopyCustomResource%7C00112233-4455-6677-8899-aabbccddeeff?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220101T000000Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7199&X-Amz-Credential=ABCDEFGHIJKLMNOPQRST%2F20220101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
}

So if anything the constructor seems to provide a better break down of the URL.因此,如果构造函数似乎可以更好地分解 URL。 I don't why, when I try to copy the parsedUrl.path field to options.path it works when parsedUrl comes from the parse() method but not when it comes from the constructor.我不知道为什么,当我尝试将 parsedUrl.path 字段复制到 options.path 时,它在 parsedUrl 来自 parse() 方法时有效,但在它来自构造函数时无效。 The hostname field on the other hand works in both cases.另一方面,主机名字段在这两种情况下都有效。

Any idea what's the issue here?知道这里有什么问题吗?

As indicated by the OP, the use of the url.parse method is discouraged in favor of the WATWG URL API .如 OP 所示,不鼓励使用url.parse方法,而建议使用WATWG URL API The legacy .path attribute returns the combined pathname and search components.旧的.path属性返回组合的pathnamesearch组件。 Although the preferred WATWG URL API does not have the path attribute, the value required by options.path can be constructed by combining the .pathname and .search attributes.虽然首选 WATWG URL API 没有path属性,但是options.path所需的值可以通过组合.pathname.search属性来构造。

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

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