Kamil Kamili
Kamil Kamili

Reputation: 1927

Using multiple router defined in their own files in express

I am new to Node.js and express framework. I am getting an error when I am trying to go to the signuplogin route from my homepage route. I have also tried all the similar questions in the stackoveflow, but none of them have worked for me, so I thought of posting my own.

This is my app.js

var homepage = require('./routes/homepage');
var signupLogin = require('./routes/signuplogin');

var app = express();

app.use("/", homepage);
app.use("/signup-login", signupLogin);

This is my homepage.js in routes directory

var express = require('express');
var homepageRouter = express.Router();

/* GET Homepage. */
homepageRouter.get("/", function(req, res, next) {
  res.render('../views/homepage/homepage.hbs');
});

module.exports = homepageRouter;

This is my signup-login.js in routes directory

var express = require('express');
var signupLoginRouter = express.Router();

/* GET Signup Login Page. */
signupLoginRouter.get("/signup-login", function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = signupLoginRouter;

Well my "/" route works perfectly but it says 404 when I try to access "/signup-login" route.

I also changed the route to "/signuplogin" because I thought maybe it doesn't except the "-" character in the route. But that didn't work as well. Any solution/advice?

Upvotes: 1

Views: 49

Answers (1)

Ashvin777
Ashvin777

Reputation: 1482

You have done a mistake in your signup-login.js file. Correct the code with this -

/* GET Signup Login Page. */
signupLoginRouter.get("/", function(req, res, next) {
  res.send('respond with a resource');
});

As per your code the actual url of signup page becomes to "/signup-login/signup-login"

You don't need to add the singup-login url path again in your page. That is already referenced in your main router.

Upvotes: 2

Related Questions