Search code examples
javascriptnode.jsexpressurl-routing

Express js routing returns 404


What i need

This is a question about express.post() routing returning 404 state.

What i have

I have this code in my server.js and it is ok (ermm, i guess).

var bcrypt = require('bcryptjs');
var bodyParser = require('body-parser');
var cors = require('cors');
var express = require('express');
var jwt = require('jwt-simple');
var moment = require('moment');
var mongoose = require('mongoose');
var path = require('path');
var request = require('request');
var compress = require('compression');

var config = require('./config');

var User = mongoose.model('User', new mongoose.Schema({
  futibasId: { type: String, index: true },
  email: { type: String, unique: true, lowercase: true },
  password: { type: String, select: false },
  username: String,
  fullName: String,
  picture: String,
  accessToken: String
}));
mongoose.connect(config.db);

var app = express();

app.set('port', process.env.PORT || 80);
app.use(compress());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 2628000000 }));

/*
 |--------------------------------------------------------------------------
 | Login Required Middleware
 |--------------------------------------------------------------------------
 */
function isAuthenticated(req, res, next) {
  if (!(req.headers && req.headers.authorization)) {
    return res.status(400).send({ message: 'You did not provide a JSON Web Token in the Authorization header.' });
  }

  var header = req.headers.authorization.split(' ');
  var token = header[1];
  var payload = jwt.decode(token, config.tokenSecret);
  var now = moment().unix();

  if (now > payload.exp) {
    return res.status(401).send({ message: 'Token has expired.' });
  }

  User.findById(payload.sub, function(err, user) {
    if (!user) {
      return res.status(400).send({ message: 'User no longer exists.' });
    }

    req.user = user;
    next();
  });
}

/*
 |--------------------------------------------------------------------------
 | Generate JSON Web Token
 |--------------------------------------------------------------------------
 */
function createToken(user) {
  var payload = {
    exp: moment().add(14, 'days').unix(),
    iat: moment().unix(),
    sub: user._id
  };

  return jwt.encode(payload, config.tokenSecret);
}

/*
 |--------------------------------------------------------------------------
 | Sign in with Email
 |--------------------------------------------------------------------------
 */
app.post('/auth/login', function(req, res) {
  User.findOne({ email: req.body.email }, '+password', function(err, user) {
    if (!user) {
      return res.status(401).send({ message: { email: 'Incorrect email' } });
    }

    bcrypt.compare(req.body.password, user.password, function(err, isMatch) {
      if (!isMatch) {
        return res.status(401).send({ message: { password: 'Incorrect password' } });
      }

      user = user.toObject();
      delete user.password;

      var token = createToken(user);
      res.send({ token: token, user: user });
    });
  });
});

My node is running on this file normally, but my routing just wont work.

What i did

I tried to debug putting this on my homepage:

<form action="http://localhost/futibas/auth/login/" method="post"><input type="hidden" value="teste" /><input value="submit" type="submit"/></form>

But when i press the submit button, i get this in my network tab:

Request URL:http://localhost/futibas/auth/login/
Request Method:POST
Status Code:404 Not Found

And if i make an ajax request, i got this return

POST http://localhost/futibas/auth/login 404 (Not Found)

I even tried to change the express.post path to absolute, but nothing.

app.post('http://localhost/futibas/auth/login', function(req, res) {

I just can't make it work. Please, someone help me! (:

...

EDIT

AS @Nonemoticoner has said, I changed

app.post('/auth/login', function(req, res) {

to

app.post('/futibas/auth/login', function(req, res) {

but still getting a 404


Solution

  • Solution by OP.

    I installed python and used

    python -m SimpleHTTPServer
    

    into my /client directory, then, accessing via "localhost:8000" it all worked.