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"));
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With