用node写了一个后台,但是post提交数据的时候总找不到路由,是什么原因?
发布于 6 年前 作者 duanyong 7609 次浏览 来自 官方Issues

https://github.com/jasonlee123man/node.git

router/admin/add.js

const express = require("express");
var router = express.Router()
 
router.get("/", function (req, res) {
    res.render("add")
})
 
router.post("/doAdd", function (req, res) {
    //1.接收数据
    var form = new multiparty.Form();
 
    //指定上传目录
    form.uploadDir = "upload";
 
    form.parse(req, function (err, fields, files) {
        var realname = fields.realname[0];
        var gender = fields.gender[0];
        var age = fields.age[0];
        var address = fields.address[0];
        //通过filles拿到图片路径
        var image = files.image[0].path;
 
        //2. 把数据添加到数据库
        MongodbClient.connect(dbUrl, {
            useUnifiedTopology: true
        }, function (err, client) {
            var collection = client.db("mydb").collection("student");
            collection.insertOne({
                realname: realname,
                gender: gender,
                age: age,
                address: address,
                image: image
            }, function (err, result) {
                if (err) {
                    console.log(err);
                    return;
                }
                res.send("<script>alert('成功添加');location.href='/list'</script>")
            })
        })
    })
})
module.exports = router;
回到顶部