I am getting an error (TypeError: Cannot read properties of undefined (reading 'use')) when I try to run the 'node server.js' cmd from the Terminal. It shows an error in my auth.routes.js file.
Here is the content of my 'auth.routes.js' file:
import verifySignUp from "../middleware/index.js";
import controller from "../controllers/auth.controller.js";
export default function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
app.post(
"/api/auth/signup",
[
verifySignUp.checkDuplicateUsernameOrEmail,
verifySignUp.checkRolesExisted
],
controller.signup
);
app.post("/api/auth/signin", controller.signin);
};
I refer to it in the 'server.js' file. See below:
// routes
import authRoute from './app/routes/auth.routes.js';
import userRoute from './app/routes/user.routes.js';
app.route = authRoute();
app.route = userRoute();
Why are you creating "route" property on "app" object? You don't have to set properties to express's "app" object. What treats to your error - you need to organize your code in this way:
// auth.routes.js
import verifySignUp from "../middleware/index.js";
import controller from "../controllers/auth.controller.js";
export default function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
app.post(
"/api/auth/signup",
[
verifySignUp.checkDuplicateUsernameOrEmail,
verifySignUp.checkRolesExisted
],
controller.signup
);
app.post("/api/auth/signin", controller.signin);
};
// server.js
import initializeAuthRoute from './app/routes/auth.routes.js';
import initializeUserRoute from './app/routes/user.routes.js';
initializeAuthRoute(app);
initializeUserRoute(app);