简体   繁体   中英

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.

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}

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.
  • However, Backticks(``) will evaluate the expression inside ${} and then output the evaluated value.

Hope this solves your issue !

Wrong quotes

Use ``

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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