使用 Volley GET 方法通过 Node js 插入数据库 API

Using Volley GET method to insert in database with Node js API

我实际上是在使用 volley 通过 Node js webservice 插入数据库。 这是我的节点 js 代码:

var express = require('express');
var router = express.Router();
var url = require('url');
router.get('/',function (req,res,next) {
    var queryData = url.parse(req.url,true).query;
    var mysql = require('mysql');
    var config = {
        host : 'localhost',
        user : 'root',
        password : '',
        database : 'travelmatedb'
    }
    var connection = mysql.createConnection(config);
    connection.connect();
    connection.query("INSERT INTO feedback (stars,description) VALUES ('"+queryData.stars+ "', '"+queryData.description+ "')",
        function(err,rows,fields){
       if (!err){
           var signup = {msg:"thanks , feedback added"}
           res.send(signup);
    }
    else
    {
        var error = {msg:"error cannot execute query"}
        res.send(err);
    }
});
    connection.end();
});
module.exports = router;

这是我的 android 代码:

                String URL = "http://10.0.2.2:3000/addfeedback?stars="+String.valueOf(ratingBar.getRating()+"&description="+description.getText().toString());
                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                JsonObjectRequest objectRequest = new JsonObjectRequest(
                        Request.Method.GET,
                        URL,
                        null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                System.out.println("trip added");
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("Rest Response",error.toString());
                            }
                        }
                );


                requestQueue.add(objectRequest);

它已经正确地插入到数据库中,但我的问题是在 volley 中使用 GET 方法安全地插入、更新或删除?它不应该是 POST 方法?那么如果它应该使用 POST 方法,为什么它使用 GET 方法?这在部署应用程序时有任何副作用吗?

我是这里的新手,所以请原谅我缺乏经验,谢谢大家。

看看你的 NodeJS / Express 模块,我不是专家,但我会考虑重新设计类似的东西:

var express = require('express');
var router = express.Router();
var url = require('url');

//TODO: Get these parameters from process.env.XXXX
var config = {
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'travelmatedb'
}
var mysql = require('mysql');
var pool = mysql.createPool(config);

var INSERT_SQL = "INSERT INTO feedback (stars, description) VALUES (:stars, :desc)";

// Changed to accept a POST request, respect RESTFul
router.post('/', function (req, res, next) {
  var queryData = req.query; // Express already parses the Query Params into the request.query object

  //TODO: Validate that the supplied Parameters are there

  if (!req.query.stars) {
    return res.status(400).json({msg: "`stars` is required"});
  }

  pool.query(
    INSERT_SQL, {
      "stars": queryData.stars,
      "desc": queryData.description
    },
    function (err, rows, fields) {
      if (!err) {
        var signup = {
          msg: "thanks , feedback added"
        }
        res.json(signup);
      } else {
        var error = {
          msg: "error cannot execute query"
        }
        console.log(err.message, err); //Print out that actual error
        res.status(500).json(error);
      }
    });

});

module.exports = router;

参考:https://www.npmjs.com/package/mysql#pooling-connections