简体   繁体   English

如何使用node.js在前端打印console.log并表达

[英]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. 我正在学习node.js并表示要进行副项目。

How can I print console.log(); 如何打印console.log(); on the client-side? 在客户端?

I'm working on a Hello project and I have the following code: 我正在开发一个Hello项目,并且有以下代码:

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: 为什么执行以下操作时console.log()不在客户端打印:

app.get('/', (req, res) => {
  res.send(console.log("test"));
});

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

res.send accepts an argument which is of the following types: res.send接受以下类型的参数:

a Buffer object, a String, an object, or an Array 缓冲区对象,字符串,对象或数组

console.log does not return anything and therefore nothing is passed to res.send . console.log不返回任何内容,因此没有任何内容传递给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. 这将输出脚本元素,该脚本元素会将某些内容记录到客户端控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript node.js中的console.log打印函数内的变量列表 - how can I print a list of variables within a function using console.log in javascript node.js 如何使用 node.js(express) API 调用访问搜索参数并在 React 前端提供服务 - How to access search params with node.js(express) API call and served on react front-end Console.log 在 dockerized Node.js / Express 应用程序中不起作用 - Console.log not working in a dockerized Node.js / Express app Twitter Oauth Node.js Express console.log 未显示 - Twitter Oauth Node.js Express console.log not showing 如何在渲染中显示而不是在node.js中显示console.log - How do i display on render instead of console.log in node.js 如何将大型 JSON object 转换为 Node.js 中类似于 console.log() 的字符串 - How do I convert a large JSON object to a string looking like console.log() in Node.js 如何使用node.js来运行react.js前端? - how do use node.js to run react.js front-end? 如何使用 node.js 更改前端 html - How to change front-end html using node.js 如何将后端(Node.js)链接到前端(React) - How to LInk Backend(Node.js) to front-End(React) 如何自动完成在我的Node.js项目中编译Twitter Bootstrap等前端框架的任务? - How do I automate the task of compiling front-end frameworks like Twitter Bootstrap in my Node.js project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM