Crown Wins
Crown Wins

Reputation: 35

Writing web data to a TXT file

(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

Answers (1)

Brad
Brad

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

Related Questions