Hello first time here and I am practicing node and express js with a udemy course project. I initially ran the solution code and it worked fine. When i started to create my own file and followed the steps of the solution code I started getting this weird type error. Then i tried running the solution code again and now the solution has the same type error even though it worked fine before.
I do have the json packages installed and npm i as well. Is there something I haven’t installed, node is up to date and I have not reassigned the constant variable; since nothing has changed i don’t understand how it ran fine before and now its not.
import express from "express";
import bodyParser from "body-parser";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const port = 3000;
var userIsAuthorised = false;
app.use(bodyParser.urlencoded({ extended: true }));
function passwordCheck(req, res, next) {
const password = req.body["password"];
if ((password = "ILoveProgramming")) {
userIsAuthorised = true;
}
next();
}
app.use(passwordCheck);
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
app.post("/check", (req, res) => {
if (userIsAuthorised) {
res.sendFile(__dirname + "/public/secret.html");
} else {
res.sendFile(__dirname + "/public/index.html");
//Alternatively res.redirect("/");
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
This is the error:
TypeError: Assignment to constant variable.
at passwordCheck (file:///Users/cassidypriestley/Desktop/3.5%20Secrets%20Project%202/solution.js:16:17)
at Layer.handle [as handle_request] (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:328:13)
at /Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:286:9
at Function.process_params (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:346:12)
at next (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:280:10)
at urlencodedParser (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:328:13)
at /Users/cassidypriestley/Desktop/3.5 Secrets Project 2/node_modules/express/lib/router/index.js:286:9
I have verified the node version i am currently using and even tried the direct solution given by the course instructor and it seems the instructor’s solution file is now giving me the same type error as my index.js file.
Cassidy Marie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1