Express js 路由 returns 404

Express js routing returns 404

我需要什么

这是一个关于 express.post() 路由 returning 404 状态的问题。

我有什么

我的 server.js 中有此代码,没问题(呃,我猜)。

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 });
    });
  });
});

我的节点在这个文件上正常 运行,但我的路由不起作用。

我做了什么

我试着调试把它放在我的主页上:

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

但是当我按下提交按钮时,我在我的网络选项卡中看到了这个:

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

如果我提出 ajax 请求,我会得到这个 return

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

我什至尝试将 express.post 路径更改为绝对路径,但什么也没有。

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

我就是做不到。请有人帮助我! (:

...

编辑

正如@Nonemoticoner 所说,我改变了

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

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

但仍然收到 404

您为 /auth/login 创建了一个 POST 路由,而不是 /futibas/auth/login。所以基本上这就是为什么 returns 404.

OP 的解决方案。

我安装了python并使用了

python -m SimpleHTTPServer

进入我的 /client 目录,然后通过 "localhost:8000" 访问一切正常。