[英]Express js routing returns 404
This is a question about express.post() routing returning 404 state. 这是有关express.post()路由返回404状态的问题。
I have this code in my server.js and it is ok (ermm, i guess). 我的server.js中有此代码,可以(我猜是ermm)。
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. 我的节点正常在此文件上运行,但是我的路由无法正常工作。
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 如果我提出ajax请求,我会得到这个回报
POST http://localhost/futibas/auth/login 404 (Not Found)
I even tried to change the express.post path to absolute, but nothing. 我什至尝试将express.post路径更改为绝对路径,但是什么也没有。
app.post('http://localhost/futibas/auth/login', function(req, res) {
I just can't make it work. 我只是无法使其工作。 Please, someone help me!
拜托,有人帮我! (:
(:
... ...
AS @Nonemoticoner has said, I changed AS @Nonemoticoner说过,我改变了
app.post('/auth/login', function(req, res) {
to 至
app.post('/futibas/auth/login', function(req, res) {
but still getting a 404 但仍然得到404
You created a POST routing for /auth/login
- NOT /futibas/auth/login
. 您为
/auth/login
/futibas/auth/login
不是/futibas/auth/login
创建了POST路由。 So basically it's why it returns 404. 所以基本上就是为什么它返回404的原因。
Solution by OP. 由OP解决。
I installed python and used 我安装了python并使用
python -m SimpleHTTPServer
into my /client directory, then, accessing via "localhost:8000" it all worked. 进入我的/ client目录,然后通过“ localhost:8000”访问就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.