我在一个存储库中有一些用于API的expressJS代码(返回),我想在我的路由'/ login'上使用axios在另一个存储库(ReactJS - Front)上进行POST调用,但它没有'工作并返回404错误代码。
App.js(API):
const express = require('express');
const app = express()
const PORT = process.env.PORT || 3001
const bodyParser = require('body-parser')
const mysql = require('mysql');
const bdd = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database: 'football'
});
bdd.connect();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// My other routes
//LOGIN
app.post('/login',(req,res)=>{
const mail = req.body.mail
const password = req.body.password
bdd.query(`SELECT * FROM user WHERE mail = '${mail}' AND password = '${password}'`, function(err, row, field){
res.send(row)
})
})
app.use((req,res,next)=>{
res.status(404);
res.json({code:'NOT_FOUND',message:'Page not found'});
});
app.listen(PORT)
module.exports = app
App.js(Front):
import React, { Component } from 'react';
import axios from 'axios';
class Login extends Component {
constructor(props) {
super(props);
this.state = {
mail: null,
password: null
};
}
login = () => {
if(this.state.mail != null && this.state.password != null){
axios.post('http://127.0.0.1:3001/login',{
mail : this.state.mail,
password : this.state.password
}).then(res)=>{
console.log(res);
};
}
}
export default Login;