POST http://localhost:3000/user/login 404 (Not Found),前端登录页面显示404,后台接口跟前端数据对接都没问题,求大佬解决???
const submitForm = (formEl) => {
// if (!formEl) return;
// formEl.validate((valid) => {
// console.log(222);
// if (valid) {
register({ username: ruleForm.username, password: ruleForm.password }).then((res) => {
if (res.status == "success") {
ElMessage({
message: res.message,
type: "success",
});
router.push("/home");
} else {
ElMessage({
message: data.message,
type: "error",
});
}
})
.catch((err) => {
ElMessage({
message: err.message,
type: "error",
});
});
// 用户登录
export function register(data) {
return service({
url: '/user/login',
method: 'post',
data
})
}
//登录
exports.loginUser = (req, res) => {
const { username,password } = req.body
if (username === 'admin' && password === '123456') {
res.json({ message: '登录成功', user: username });
} else {
res.status(401).json({ message: '登录失败,用户名或密码错误' });
}
}
let express = require("express");
let router = express.Router();
// let user = require("./API/user");
// let dept = require("./API/dept");
// let course = require("./API/course");
// let score = require("./API/score");
// let home = require("./API/home");
let login = require("./API/login");
// --------------------------登录相关-----------------------------------------------
// 列表
router.get('/user/login', login.loginUser);
module.exports = router;
let express = require("express");
let history = require('connect-history-api-fallback'); //用于处理路由配置 mode:history,强制刷新问题
let app = express();
let cors = require("cors");
// 导入 body-parser中间件解析表单数据
let bodyParser = require("body-parser");
let router = require("./router");
// 解析 url-encoded格式的表单数据
app.use(bodyParser.urlencoded({ extended: false }));
// 解析json格式的表单数据
app.use(bodyParser.json());
app.use(cors()); //配置跨域,必须在路由之前
app.use(router) //配置路由
app.use(history()) //用于处理路由配置 mode:history,强制刷新问题
app.use(express.static(__dirname+'/static'))
app.listen(3000, () => {
console.log("服务器启动成功");
});
我前端后端分别测试了一遍还是找不出问题,希望页面成功登录