gpt4 book ai didi

javascript - 如何从 Node 响应对象获取请求 URL?

转载 作者:行者123 更新时间:2023-12-04 10:13:13 26 4
gpt4 key购买 nike

当您使用 Node 发出 http/https 请求(或使用诸如 simple-get 之类的包装器)时,您将返回 err , response , 和 data对象。有没有办法在回调中获取原始请求 URL、主机、url 等?

示例(使用 simple-get 's concat ):

const get = require('simple-get')

get.concat('http://example.com', function (err, res, data) {

if (err) throw err

console.log(res.statusCode) // 200
console.log(res.headers) // {...}

// i want "http://example.com" at this point
console.log(res.uri) // undefined
console.log(res.url) // undefined
console.log(res.path) // undefined
console.log(res.host) // undefined

})

最佳答案

您可以从底层套接字获取一些信息(因为 simple-get 只是 http -module 的包装)和 req -目的:

get.concat('https://www.google.com/search/howsearchworks/algorithms/', (err, res, data) => {
if (err) throw err
console.log(res.socket.servername); // prints www.google.com
console.log(res.req.path); // prints search/howsearchworks/algorithms
})

关于javascript - 如何从 Node 响应对象获取请求 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61214955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com