我正在创建一个简单的应用程序来练习将Vue连接到Express服务器。我有一个表单,我试图发送到后端,但我似乎无法将我的数据发送到后端。 我收到的错误是:将Vue连接到Express - 404 Not Found
POST http://localhost:8080/login 404 (Not Found)
我最好的猜测是,在我的Vue公司的方法找不到我的服务器上的一个匹配的路由?如果是这样,我很困惑,因为我有登录路线。
在我的Vue脚本:
const axios = require('axios');
export default {
data: function() {
return {
user: {
email: '',
password: ''
}
}
},
methods: {
sub() {
var user = {
email: this.user.email,
password: this.user.password
}
axios.post('/login', user)
.then(res => console.log(res))
.catch(err => console.log(err))
}
}
}
论后端:
const path = require('path');
const express = require('express');
const app = express();
app.use(express.static(path.join(__dirname, '..')));
app.post('/login', function(req, res) {
console.log("Server HIT!!!!!!!!!!!!!!!!!!!!")
})
app.get('*', function (req, res) {
return res.sendFile('../index.html');
});
app.listen(3000);
console.log('Express server listening on port 3000');
谢谢!从我的谷歌搜索中,我知道它需要中间件,但我没有意识到它进入了它自己的功能。我打我的服务器现在没有问题! – Ziyal
很高兴帮助你:) –