Azure移动应用程序node.js后端-自定义查询和传递参数 [英] Azure Mobile App node.js backend - Custom queries and passing parameters
问题描述
我曾经有一个带有JavaScript后端的Azure移动服务.现在,我切换到带有其node.js后端的新Azure移动应用程序. 由于node.js对事情的处理方式不同,所以我想问你如何做一些简单的事情,这些事情以前可以在较旧的后端上使用.
I used to have an Azure Mobile Service with a JavaScript backend. Now I switched to the new Azure Mobile App with its node.js backend. Since things are handled differently with node.js, I want to ask you how to do some simple things that used to work with the older backend.
第一件事是,如何在服务器端修改读取"或插入"请求,例如执行自定义查询.我以前是这样的:
The first thing is, how can I modify a "read" or "insert" request on the server side to, for instance, execute a custom query. I used to do that like this:
function read(query, user, request) {
var visitorID = request.parameters.visitor;
var liked = request.parameters.liked;
var link = request.parameters.link;
if (visitorID && !liked && !link) {
if (visitorID.indexOf(" ") == -1) {
var sqlUnique = "SELECT TOP 1 * FROM ........;";
mssql.query(sqlUnique, {
success: function(results) {
if(results.length > 0) {
request.respond(statusCodes.OK, results);
}
else {
request.respond(statusCodes.BAD_REQUEST, {error: 'An error message.'});
}
},
error: function(err) {
request.respond(statusCodes.BAD_REQUEST, {error: 'There was a problem with the request.'});
}
});
} else {
request.respond(statusCodes.BAD_REQUEST, {error: 'Invalid username.'});
}
}
else {
// no server-side action needed
request.execute();
}
}
如您所见,每当客户端从服务器读取时,其他一些事情就会被执行.我的问题是,现在如何使用Azure移动应用程序的node.js后端执行相同的操作.尤其是当客户端在请求内传递某些参数时.如何在node.js中读取这些参数?在请求中执行查询之前,如何执行自定义查询?
As you can see, whenever a client reads from the server, some other things are getting executed. My question is now, how to do that same thing with Azure Mobile App's node.js backend. Especially when a client passes some parameters within the request. How can I read those parameters in node.js? And how can I execute a custom query before executing the query in the request?
如果有人可以帮助我将上述JavaScript代码移植到node.js,我将非常高兴!
If someone can help me port the above JavaScript code to node.js, I would be very happy!
推荐答案
如果我没有误解,您想在Mobile Apps的EasyTables脚本中的表操作中查询一些自定义SQL stmt.
If I don't misunderstand, you want to query some custom SQL stmt in table operations in EasyTables scripts in Mobile Apps.
我们可以利用"use()"自定义中间件来指定针对表的每个请求要执行的中间件,作为对azure-mobile-apps sdk文档的描述,位于
we can leverage "use()" to custom middleware to specify middleware to be executed for every request against the table as the description on the document of azure-mobile-apps sdk at http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use.
var queries = require('azure-mobile-apps/src/query');
var readMiddleware = function(req,res,next){
var table = req.azureMobile.tables('table2'),
query = queries.create('table2')
.where({ TestProperty : req.body.testproperty });
table.read(query).then(function(results) {
if(results){
req.someStoreData = somehander(results); //some hander operations here to get what you want to store and will use in next step
next();
}else{
res.send("no data");
}
});
};
table.read.use(readMiddleware, table.operation);
table.read(function (context) {
console.log(context.req.someStoreData);
return context.execute();
});
You can refer to the similar question on SO, Query other tables in insert function of Azure Table Storage.
您可以通过context.req.query.someparams
在表操作关闭中获取参数,类似于我们在expressjs中处理参数.
您可以参考代码示例在GitHub上.
And you can get the params in table operations closure via context.req.query.someparams
similar with we handling params in expressjs.
You can refer to the code sample on GitHub.
这篇关于Azure移动应用程序node.js后端-自定义查询和传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!