小白求助,express的get请求发送失败什么原因?

白贝无暇
  • 3
新手上路,请多包涵
//导入数据库
var mysql = require("mysql");
var express = require("express");
var app = express();
app.use('/public', express.static('public'));

//创建连接对象
var connection = mysql.createConnection({
    host: '********',
    user: '****',
    password: '*****',
    port: '****',
    database: '*****'
})

//获取用户列表
app.get('/users', function (req, res) {
    connection.connect();

    //操作数据库
    var sql = `select * from users`;
    connection.query(sql, [], function (err, result) {
        if (err) {
            console.log("连接失败")
            console.log('[select error - ', err.message)
        } else {
            console.log("连接成功");
            console.log(result)
            res.send(result)
        }
    });
    connection.end();
})

//添加一个新数据
app.get('/adduser/:userphone', function (req, res) {
    connection.connect();

    console.log(req.params)
    var sql = `insert into users(userphone) values (${req.params.userphone.toString()})`;
    connection.query(sql, [], function (err, result) {
        if (err) {
            console.log("添加失败")
            console.log('[select error - ', err.message)
        } else {
            console.log("添加成功");
            console.log(result)
            res.send(result)
        }
    });
    connection.end();

})

//更新多个数据
app.get('/updateuser/:userid/:userphone/:username:/:userpassword/:useremail', function (req, res) {
    connection.connect();

    var sql = `
    update users set userphone = ${req.params.phone},username = ${req.params.username}, userpassword = ${req.params.userpassword},useremail = ${req.params.useremail} 
    where userid = ${req.params.userid}`;
    connection.query(sql, [], function (err, result) {
        if (err) {
            console.log("更新失败");
            console.log('[select error - ', err.message);
            res.end(error);
        } else {
            console.log("更新成功");
            console.log(result);
            res.send(result);
        }
    })
    connection.end();

})


app.listen(8080)

前面两个功能都是正常的,但是更新操作总是cannot get

回复
阅读 171
1 个回答
app.get('/updateuser/:userid/:userphone/:username:/:userpassword/:useremail'
//                                               ^ 这里多了一个冒号

看看是不是这个冒号的原因

你知道吗?

宣传栏