1

I am doing the Express crash course, and my first clue that something was wrong is when I ran

app.listen(PORT, () => console.log('Server started on port ${PORT}'));  

and it didn't pass the port number through. the problem persists in this area where it simply crashes the server:

const logger = (req, res, next) =>{
console.log('${req.protocol}://${req.get('host')}${req.originalUrl}');
next();
};

Altogether now, here is the full index.js:

const express = require('express');
const path = require('path');
const members = require('./Members');
const app = express();

const logger = (req, res, next) =>{
console.log('${req.protocol}://${req.get('host')}${req.originalUrl}');
next();
};

app.use(logger);

app.get('/api/members', (req, res)=> {
res.json(members);

});

app.use(express.static(path.join(__dirname, 'public')));

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

app.listen(PORT, () => console.log('Server started on port ${PORT}')); 

The error:

   console.log('${req.protocol}://${req.get('host')}${req.originalUrl}');
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: missing ) after argument list
        at Module._compile (internal/modules/cjs/loader.js:723:23)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
        at Module.load (internal/modules/cjs/loader.js:653:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
        at Function.Module._load (internal/modules/cjs/loader.js:585:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
        at startup (internal/bootstrap/node.js:283:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

2 Answers 2

1

Template strings do use back-ticks:

Template literals are enclosed by the back-tick (``)

Therefore

console.log('${req.protocol}://${req.get('host')}${req.originalUrl}');

should be

console.log(`${req.protocol}://${req.get('host')}${req.originalUrl}`);

and

app.listen(PORT, () => console.log('Server started on port ${PORT}'));  

should be

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));  
Sign up to request clarification or add additional context in comments.

Comments

0

This is because you are using '' which is for an arbitral string... for the port to be displayed, use ``

1 Comment

Ok this grave character is new to me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.