I am creating a web server that will allow users make a request for fortunes, and get the data which I specified in my fortunes.json file. I am using express in node to do this.
I start my server this way npm run dev
, however when I do so, I get the following error:
SyntaxError: Unexpected token const
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
Does anybody have an idea what I am doing wrong, my syntax, and packages.json file, etc. all seems to be fine but I am still getting this.
I am expecting to see "server listening on port 3000" when I run the server however I get a token error.
const express = require('express');
const port = 3000,
const app = express();
app.get('/fortunes', (req,res) => {
console.log('requesting fortunes');
});
app.listen(port, () => console.log('Listening on port ${port}'));
You have two issues in your code.
1 . when you are initializing variable:
As you have put ',' (comma) thus you cannot specify its declaration again.
your code
const port = 3000,
const app = express();
Do like this :
const port = 3000,
app = express();
or use this :
const port = 3000;
const app = express();
app.listen(port, () => console.log(`Listening on port ${port}`));
Try below Code:
const express = require('express');
const port = 3000,
app = express();
app.get('/fortunes', (req,res) => {
console.log('requesting fortunes');
});
app.listen(port, () => console.log(`Listening on port ${port}`));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With