Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print console.log on the front-end side with node.js and express

I'm learning node.js and express for a side-project.

How can I print console.log(); on the client-side?

I'm working on a Hello project and I have the following code:

app.get('/', (req, res) => {
  res.send('Hello from App Engine!');
});

Why do console.log() doesn't print in the client-side when I'm doing this:

app.get('/', (req, res) => {
  res.send(console.log("test"));
});
like image 973
Simon Breton Avatar asked Dec 27 '25 16:12

Simon Breton


1 Answers

https://expressjs.com/en/api.html#res.send

res.send accepts an argument which is of the following types:

a Buffer object, a String, an object, or an Array

console.log does not return anything and therefore nothing is passed to res.send. Instead, you can do the following:

res.send(`
     <!DOCTYPE html>
     <html>
     <body>
        <script>
          console.log(${ "test" /* this can be variable instead */ });
        </script>
     </body>
     </html>
`);

This will output a script element that will log something to the client console.

like image 124
JBis Avatar answered Dec 31 '25 18:12

JBis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!