Fetch only headers of a GET request in Node

后端 未结 3 1738
感情败类
感情败类 2021-01-15 18:40

I need to get the Content-Length and Content-Type headers of large files with Node.

Unfortunately, the server I\'m dealing with do

3条回答
  •  别那么骄傲
    2021-01-15 19:05

    Use method: 'HEAD':

    http.request('http://example.com', {
        method: 'HEAD',
    }, res => {
        console.log(res.statusCode, res.statusMessage)
        console.log(res.headers)
    
        res.on('data', _ => {
            console.log(`IT SHOULDN'T REACH HERE!`)
        })
    }).on('error', console.error)
      .end()
    

提交回复
热议问题