Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

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?
Node.jsJavaScript

Avatar of undefined
Last Comment
Chinmay Patel

8/22/2022 - Mon
Chinmay Patel

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 Gust

ASKER
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}"));
ASKER CERTIFIED SOLUTION
Zakaria Acharki

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chinmay Patel

Hi Bruce,

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

Regards,
Chinmay.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bruce Gust

ASKER
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 Patel

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 Patel

It seems I am wrong again ... template literals are very well supported in JS as well.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.