Node.js 如何将多个参数传递到RESTful API中进行查询?
我有一个node.js程序 我想使用node.js程序执行如下查询:Node.js 如何将多个参数传递到RESTful API中进行查询?,node.js,restful-url,Node.js,Restful Url,我有一个node.js程序 我想使用node.js程序执行如下查询: SELECT * from users where uid = ? and role = ? http://localhost:4001/api/v1/getUserByUID?uid=XXXXX&role=XXXXX 我想设计如下的URL: SELECT * from users where uid = ? and role = ? http://localhost:4001/api/v1/getUserByU
SELECT * from users where uid = ? and role = ?
http://localhost:4001/api/v1/getUserByUID?uid=XXXXX&role=XXXXX
我想设计如下的URL:
SELECT * from users where uid = ? and role = ?
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;
显然,您需要确保查询参数已清理,等等。试试这个
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;
显然,您需要确保查询参数已清理,等等。在代码中,
:uid
的可能重复被映射到。根据您提到的URL,您希望将路由URL更改为/api/v1/getUserByUID
,并遵循@Sahil posted链接。在您的代码中,:uid
的可能重复将映射到。根据您提到的URL,您希望将路由URL更改为/api/v1/getUserByUID
,并遵循@Sahil posted链接。您能告诉我如何使用改型调用此api吗?您能告诉我如何使用改型调用此api吗?