如何将多个参数传入RESTfulAPI进行查询?
How to pass multiple parameters into RESTful API for query?
我有一个node.js程序。
我想使用这个 node.js 程序来执行如下查询:
SELECT * from users where uid = ? and role = ?
我想设计 URL 如下:
http://localhost:4001/api/v1/getUserByUID?uid=XXXXX&role=XXXXX
如何修改以下代码来实现我的目标?
var express = require('express');
var router = express.Router();
router.get('/api/v1/getUserByUID/:uid', function(req, res, next) {
connection.query('SELECT * from users where uid = ?', [req.params.uid], function (error, results, fields) {
if(error){
res.send(JSON.stringify({"status": 500, "error": error, "response": null}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
//If there is no error, all is good and response is 200OK.
}
});
});
module.exports = router;
谢谢。
试试这个
var express = require('express');
var router = express.Router();
router.get('/api/v1/getUserByUID', function(req, res, next) {
connection.query('SELECT * FROM users WHERE uid = ? AND role = ?', [req.query.uid, req.query.role], function (error, results, fields) {
if(error){
res.send(JSON.stringify({"status": 500, "error": error, "response": null}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
//If there is no error, all is good and response is 200OK.
}
});
});
module.exports = router;
您显然想确保查询参数经过清理等
我有一个node.js程序。
我想使用这个 node.js 程序来执行如下查询:
SELECT * from users where uid = ? and role = ?
我想设计 URL 如下:
http://localhost:4001/api/v1/getUserByUID?uid=XXXXX&role=XXXXX
如何修改以下代码来实现我的目标?
var express = require('express');
var router = express.Router();
router.get('/api/v1/getUserByUID/:uid', function(req, res, next) {
connection.query('SELECT * from users where uid = ?', [req.params.uid], function (error, results, fields) {
if(error){
res.send(JSON.stringify({"status": 500, "error": error, "response": null}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
//If there is no error, all is good and response is 200OK.
}
});
});
module.exports = router;
谢谢。
试试这个
var express = require('express');
var router = express.Router();
router.get('/api/v1/getUserByUID', function(req, res, next) {
connection.query('SELECT * FROM users WHERE uid = ? AND role = ?', [req.query.uid, req.query.role], function (error, results, fields) {
if(error){
res.send(JSON.stringify({"status": 500, "error": error, "response": null}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
//If there is no error, all is good and response is 200OK.
}
});
});
module.exports = router;
您显然想确保查询参数经过清理等