Reputation: 35
(I want to use node-fetch not request)
I want to write data from a Web page (E.X google.com) to a TXT file (E.X google.txt) but it's not working. When I run the script then check my "google.txt" file all it says is [object Object]
.
Here is my code:
const fs = require('fs');
request('https://google.com', function (error, body) {
fs.writeFile('google.txt', body, (err) => {
if (err) throw err;
console.log('Wrote google.com to google.txt !');
});
});
Upvotes: 1
Views: 108
Reputation: 163232
What you call body
isn't really the HTTP response body, but the response object. Rename it so that it's clear, and then use the .body
property:
request('https://google.com', function (error, res) {
fs.writeFile('google.txt', res.body, (err) => {
if (err) throw err;
console.log('Wrote google.com to google.txt !');
});
});
Upvotes: 1