Tuesday 11 April 2017

Value of object displayed correctly in console but not in browser

I have an issue displaying the values of my object in the browser, here is my object:

websites.js

module.exports = [
    {
        url: 'value1',
        timeout: 15
    },

    {
        url: 'value2',
        timeout: 15
    }
];

and here is my simple server:

app.js

var websites = require('./websites.js'),
    http = require('http'),
    server,
    port = process.env.PORT || 3008;

server = http.createServer(function (req, res) {
    var data = "Monitoring the following: \n \n" + websites;
    console.log(websites);
    res.end(data);
}); 

server.listen(port);
console.log('Listening to port %s', port);

The console.log(websites) shows the object as

[{url: 'value1',timeout: 15},{url: 'value2',timeout: 15}]

which is fine, but when I browse http://localhost:3008/ the object is displayed as

[object Object],[object Object]

I would like my object to be displayed on the browser as it is in the console. I tried a few things like Object.values() or JSON.parse() without success.

Thank you in advance.



via Alexandre Leguay

No comments:

Post a Comment