这是关于express.post()路由返回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请求,我得到了这个回复
POST http://localhost/futibas/auth/login 404 (Not Found)
我甚至尝试将express.post路径更改为绝对路径,但没有。
app.post('http://localhost/futibas/auth/login', function(req, res) {
我无法让它发挥作用。拜托,有人帮帮我! (:
...
app.post('/auth/login', function(req, res) {
到
app.post('/futibas/auth/login', function(req, res) {
但仍然得到404
答案 0 :(得分:2)
您为/auth/login
创建了POST路由 - 不是/futibas/auth/login
。所以基本上它就是它返回404的原因。
答案 1 :(得分:1)
OP解决方案。
我安装了python并使用了
python -m SimpleHTTPServer
进入我的 / client 目录,然后通过“localhost:8000”访问它一切正常。