0

I am trying to use Express router in order to have my app.js file be less packed with code but I am having issues getting it to route to the correct place.

The page that it should be routed to is localhost:5000/users/login but it is instead routing localhost:5000/login.

I was referencing the Vidjot project from Brad Traversy's Udemy course and trying to set it up the same way but it does not seem to work even though the code is the same.

Below is the code for the app.js file and my users.js file which I am trying to route from.

// Import and initialize express.
const express = require("express");
const app = express();

// Import necessary modules.
const mongoose = require("mongoose");
const path = require("path");
const exphbs = require("express-handlebars");

// Load routes.
// const gifs = require('./routes/gifs');
const users = require("./routes/users");

// Connect to mongoose.
mongoose
  .connect("mongodb://localhost/giphy-saver", {
    useMongoClient: true
  })
  .then(() => {
    console.log("MongoDB Connected...");
  })
  .catch(err => {
    console.log(err);
  });

// Static folder.
app.use(express.static(path.join(__dirname, "public")));

// Handlebars middleware.
app.engine(
  "handlebars",
  exphbs({
    defaultLayout: "main"
  })
);
app.set("view engine", "handlebars");

// Routes.

app.get("/", (req, res) => {
  res.render("index", { title: "Giphy Saver" });
});

app.get("/about", (req, res) => {
  res.render("about", { title: "About" });
});

// Use routes.
app.use("/users", users);

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

app.listen(PORT);

const express = require("express");
const mongoose = require("mongoose");
// Used for password encryption.
const bcrypt = require("bcryptjs");
// Used for authentication.
const passport = require("passport");
const router = express.Router();

// Load the User model.
require("../models/User");
const User = mongoose.model("users");

// User login route.
router.get("/login", (req, res) => {
  res.render("users/login", { title: "Login" });
});

// User registration route.
router.get("/register", (req, res) => {
  res.render("users/register", { title: "Register" });
});

module.exports = router;

The expected result is that it should route to localhost:5000/users/login and localhost:5000/users/register instead of localhost:5000/users and localhost:5000/register. There are no error messages which are appearing.

PhysicsLemur
  • 83
  • 1
  • 1
  • 9
  • what is the response of `localhost:5000/users/login` GET request? 404? – dimitris tseggenes Jun 16 '19 at 21:33
  • I have just set up an app with the routing you have in your app and localhost:3000/users/login works as it should. I wonder if it is something like forgetting to restart your server after making changes? – user3425506 Jun 16 '19 at 21:34
  • Yes. I am getting a 404 error. – PhysicsLemur Jun 16 '19 at 22:12
  • I am using nodemon so the server is automatically restarting when I make changes. I have also manually restarted the server myself several times. The issue isn't that localhost:5000/users/login doesn't work. The issue is when I click my navbar links that should take me to the proper page they don't work. They take me to localhost:5000/login instead even though the path is the same for everything. – PhysicsLemur Jun 16 '19 at 22:13

1 Answers1

0

Are you using href="/users/login" and href="/users/register" in your html file?

Or you used href="/login" in your navbar?

ambdere
  • 71
  • 1
  • 11
  • `href="/login"` in the navbar. – PhysicsLemur Jun 17 '19 at 21:33
  • you need to use `href="/users/login"`. Your routing system will change `"/users/login" ` with the correct route. In fact when you wrote `app.use("/users", users);` you told the app to render what's inside your users route anytime it sees "/users" followed by something (i.e. login). So you need to include it – ambdere Jun 18 '19 at 14:08
  • I've tried that as well as every other combination of routes and hrefs that I could think of and it still doesn't seem to work. – PhysicsLemur Jun 19 '19 at 23:15