Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag 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?
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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.
Avatar of 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
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Bruce,

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

Regards,
Chinmay.
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!
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.
It seems I am wrong again ... template literals are very well supported in JS as well.