1step1leap
1step1leap

Reputation: 605

Express.js route not found

I am getting odd behavior for the route modules in this simple express app.

The root page '/' works but I am getting a 'not found' error for '/login'. But it works when I do app.get('/login', auth), instead of app.use('/login', auth). Any help is appreciated!

app.js

const express = require('express');
const app = express();
const port = process.env.PORT || 3000
const path    = require("path");
const bodyParser = require('body-parser')
const mongoose = require('mongoose');

const auth = require('./routes/auth');
const index = require('./routes/index');

mongoose.connect("mongodb://localhost/dev");
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use('/login', auth);
app.use('/', index);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error', { res : res });
});

app.listen(port);

./routes/index

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

router.get('/', function (req, res, next) {
  res.render('index');
});

module.exports = router;

./routes/auth

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

router.get('/login', function (req, res, next) {
  res.send('Hello world');
});

router.post('/login', function (req, res, next) {
  res.send('Goodbye world');
});

module.exports = router;

./views/index.ejs

<h1>Welcome To Our App</h1>
<a href="/login">Login</a>
<a href="/user/register">Signup</a>

./views/login.ejs

<h1>Login</h1>
<form action="/login" method="post">
  <div class="form-group">
      <label>Email</label>
      <input type="text" class="form-control" name="email">
  </div>
  <div class="form-group">
      <label>Password</label>
      <input type="password" class="form-control" name="password">
  </div>

  <button type="submit" class="btn btn-warning btn-lg">Login</button>
</form>

Upvotes: 2

Views: 9197

Answers (1)

mbrandau
mbrandau

Reputation: 554

In /routes/auth.js do router.get('/') instead of router.get('/login'). Otherwise, you would have to call localhost/login/login.

Upvotes: 7

Related Questions