【问题标题】:Why is the PORT number not getting displayed in output screen?为什么端口号没有显示在输出屏幕中?
【发布时间】:2021-02-20 09:38:22
【问题描述】:

我正在通过nodemon 命令运行服务器,我正在尝试使用${} 显示我的服务器正在运行的端口号,但是,它不是在输出屏幕中显示3000,而是显示${PORT} 即@ 987654324@ 而不是 Server running on port 3000.

const express = require('express');

const app = express();

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

app.listen(PORT, () => console.log('Server running on port ${PORT}'));

运行上述代码后在输出屏幕中显示的输出:-

[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server running on port ${PORT}

【问题讨论】:

  • 对字符串console.log(`Server running on port ${PORT}`)使用反引号(`)
  • 不是单引号(')而是反引号(1左边的键)
  • 非常感谢您的帮助。它已成功运行。

标签: javascript html node.js express socket.io


【解决方案1】:

在 JS 中,模板字符串需要反引号 ``,而常规字符串需要单引号 '' 或 Doble "" 引号。您可以使用字符串连接或模板字符串表达式。

let PORT = 80


console.log('The port is ' + PORT);
console.log(`The port is ${PORT}`);

【讨论】:

    【解决方案2】:

    不要使用单引号('),而是使用反引号(`)。反引号键可以在数字 1 的左侧找到。

    console.log(`Server is running on ${PORT}`
    

    或者您可以复制并粘贴上面的代码行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多