What is wrong with this server.js file?

Bruce Gust
Bruce Gust used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
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 Developer

Author

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}"));
Analyst Developer
Distinguished Expert 2018
Commented:
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

Ensure you’re charging the right price for your IT

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

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Bruce,

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

Regards,
Chinmay.
Bruce GustPHP Developer

Author

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 Ninja
Distinguished Expert 2018

Commented:
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 Ninja
Distinguished Expert 2018

Commented:
It seems I am wrong again ... template literals are very well supported in JS as well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial