${port} doesn't highlight as a variable

Viewed 28
import express from "express"
import mongoose from "mongoose"

const app = express();
const port = process.env.PORT || 8001;

app.get("/", (req, res) => res.status(200).send("Hello World"));

app.listen(port, () => console.log("${port}"));

console.log only gives ${port} and not 8001.

2 Answers

Use backticks instead of strings. It's called Template Literals in JS. For more details, you can visit JS documentation.

console.log(`${port}`)

Since you only use a variable, you don't need to put it into the string. You can also write it something like that:

console.log(port)

So the whole code will be:

import express from "express"
import mongoose from "mongoose"

const app = express();
const port = process.env.PORT || 8001;

app.get("/", (req, res) => res.status(200).send("Hello World"));

app.listen(port, () => console.log(port));

Template strings use backticks:

app.listen(port, () => console.log(`${port}`));

(Though, console.log(port); should also be fine here)

Related