What is wrong with this server.js file?

I'm going through a Udemy course on the MERN stack. Totally new territory for me and I've come across something that doesn't throw an error, but it's different from what should be on my screen according to the course.

Here's my code in my server.js file:

const express = require("express");

const app = express();

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

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

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

Open in new window


When I do this on GIT Bash terminal:

$ node server

I get this in response:

Server running on port ${port}

According to the course, I should be getting:

Server running on port 5000.

There are no errors and if I go out to localhost:5000, I see "Hello," but the fact that I'm getting "{port}" instead of "5000" bothers me.

What am I missing?
Bruce GustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Bruce,

This is a curious case of missing operator:
app.listen(port, () => console.log("Server running on port ${port}"));

Open in new window

should give you desired output.

But just to be clear, would you please try this:
app.listen(port, () => console.log("Server running on port " + port));

Open in new window


Regards,
Chinmay.
Bruce GustPHP DeveloperAuthor Commented:
Chimnay!

app.listen(port, () => console.log("Server running on port " + port));

...worked!

But your first suggestion did not. You mentioned a missing operator. I didn't see any difference between what I originally wrote and what you recommended. Did I miss something?

app.listen(port, () => console.log("Server running on port ${port}"));
Zakaria AcharkiAnalyst DeveloperCommented:
Hi Bruce, the javascript template literals expression ${port} you're using in :

console.log("Server running on port ${port}")

Open in new window


Should go with backticks `` instead of quotes "", like :

console.log(`Server running on port ${port}`)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Chinmay PatelChief Technology NinjaCommented:
Hi Bruce,

That comment was before I realized it was ES. Sorry for the confusion.

Regards,
Chinmay.
Bruce GustPHP DeveloperAuthor Commented:
Chinmay, Zakaria...

Thank you! The backtick dynamic worked, so we're good to go!

I"m going to go ahead and close the question, but Chinmay, what is ES and why does that make a difference?

Thanks again!
Chinmay PatelChief Technology NinjaCommented:
ES is ECMAScript https://en.wikipedia.org/wiki/ECMAScript, and the template literals are specific to ES https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals. I am not well versed with ES yet and hence the entire fiasco.
Chinmay PatelChief Technology NinjaCommented:
It seems I am wrong again ... template literals are very well supported in JS as well.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.