POST路由指示404错误(EXPRESS)

时间:2019-04-13 13:51:52

标签: node.js express router

在我的POST路由上遇到404错误,这是我在auth.js(路由)文件中所做的事情:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

这是我的app.js文件:

const express = require('express');
const router = express.Router();
const connection = require('../../helpers/db.js'); 

const bodyParser = require("body-parser");


router.use(bodyParser.json());
router.use(bodyParser.urlencoded({
    extended: true
 }));

 //create a new user


  router.post('/signup', function (req, res) {
    const insert = `INSERT INTO users ( email, password, name, lastname) values ('${req.body.email}', '${req.body.password}','${req.body.name}', '${req.body.lastname}' )`
    connection.query(insert, function (err, result) {
          if(err) {
             console.log('mysql error', error);
             res.status(500);
             res.send(error);
          } else {                  
           console.log('a new user was added!');
           res.send('Congrats, new user!');
         }
       })
     });


module.exports = router;

如果我将路线更改为GET,则可以正常工作,但是一旦执行POST路线,它就会一直告诉邮递员404错误,我尝试了很多尝试,但现在卡住了!我是这里的初学者:)

1 个答案:

答案 0 :(得分:1)

我认为您在路由中添加了前缀,因此路由为/ signup / signup

尝试更改此

app.use("/signup", authRouter); 

对此

app.use(authRouter);