Sunday, 7 May 2017

Function is called twice in nodejs

When I run this as server, and goto localhost:8080 'Saved!' is printed twice in console. And also 'Honey Bee' is appended twice in mynewfile1.txt Why is that?

var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('Hello World!');

    fs.readFile('head.html', function (err, data) {
        res.write(data);
        res.end();
    });

    fs.appendFile('mynewfile1.txt', 'Honey Bee', function (err) {
        if (err) throw err;
        console.log('Saved!');
    });
}).listen(8080);



via Ashok Dhungana

No comments:

Post a Comment