here is the site http://sf.gg, when open it, it will be redirected to the site https://segmentfault.com
use curl I can get the html code of http://sf.gg:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
I would like to know how to get it use the node module superagent?
I see Following redirects, but I don't know how to use it, or it's the right way to use it?
SuperAgent considers 3xx responses as errors by default.
So you have to handle error to get the content of the page before the redirection occurs. We can force SuperAgent to not follow the redirection with .redirects(0)
and print the response:
const superagent = require('superagent')
superagent
.get('http://sf.gg')
.redirects(0)
.on('error', err => {
console.log(err.response.text)
})
.end()
Or you can use .ok()
to act more precisely on the response:
superagent
.get('http://sf.gg')
.redirects(0)
.ok(res => {
if (res.status === 301)
console.log(res.text)
})
.end()