简体   繁体   English

在 Node.js 中,我正在使用无法正常工作的 console.log

[英]In Node.js I m using console.log which is not working

I used console.log to display a port number but it not working.It displaying string but when i use this with value it not displaying.我使用console.log 来显示端口号,但它不起作用。它显示字符串,但是当我将它与值一起使用时,它不显示。

const http = require('http');
const express = require('express');
const socketio = require('socket.io');

const PORT = process.env.PORT || 5000;

const app = express();
const server = http.createServer(app);
const io = socketio(server);

server.listen(PORT,()=>{
    return console.log('server has started on port ${PORT}');
});

I m getting Following: Output:server has started on port ${PORT}我正在关注:Output:服务器已在端口 ${PORT} 上启动

You are using it wrong.你用错了。 You must use backticks - ` and not quotes - '您必须使用反引号 - ` 而不是引号 - '

So the correct format is -所以正确的格式是 -

server.listen(PORT,()=>{
    return console.log(`server has started on port ${PORT}`);
});
  • Quotes ('') will just output whatever is given inside them.引号 ('')将只是 output 里面给出的任何内容。
  • However, Backticks(``) will evaluate the expression inside ${} and then output the evaluated value.但是, Backticks(``)将评估${}内的表达式,然后 output 评估值。

Hope this solves your issue !希望这能解决您的问题!

Wrong quotes错误的报价

Use ``使用``

console.log(`server has started on port ${PORT}`);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM