Skip to content

Instantly share code, notes, and snippets.

@dgkanatsios
Created August 31, 2016 09:28
Show Gist options
  • Save dgkanatsios/b380d1ae4195b6592c1337c63c56b2ea to your computer and use it in GitHub Desktop.
Save dgkanatsios/b380d1ae4195b6592c1337c63c56b2ea to your computer and use it in GitHub Desktop.
// ----------------------------------------------------------------------------
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------
// This is a base-level Azure Mobile App SDK.
var express = require('express'),
azureMobileApps = require('azure-mobile-apps');
var bodyParser = require('body-parser');
// Set up a standard Express app
var app = express();
app.use(bodyParser.json());
// If you are producing a combined Web + Mobile app, then you should handle
// anything like logging, registering middleware, etc. here
// Configuration of the Azure Mobile Apps can be done via an object, the
// environment or an auxiliary file. For more information, see
// http://azure.github.io/azure-mobile-apps-node/global.html#configuration
var mobile = azureMobileApps({
// Explicitly enable the Azure Mobile Apps home page
homePage: true
});
// Import the files from the tables directory to configure the /tables endpoint
mobile.tables.import('./tables');
// Import the files from the api directory to configure the /api endpoint
mobile.api.import('./api');
// Initialize the database before listening for incoming requests
// The tables.initialize() method does the initialization asynchronously
// and returns a Promise.
mobile.tables.initialize()
.then(function () {
app.use(mobile); // Register the Azure Mobile Apps middleware
app.listen(process.env.PORT || 3000); // Listen for requests
});
var api = {
"post": function (req, res, next) {
var sqlstring = 'UPDATE Highscore SET score=@score,playername=@playername WHERE id=@id';
var query= {sql:sqlstring,
parameters:[
{name:'score',value:req.body.score},
{name:'playername',value:req.body.playername},
{name:'id',value:req.body.id}]};
req.azureMobile.data.execute(query).then(function(results)
{
var sql2 = "SELECT * FROM Highscore WHERE id=@id";
var query2= {sql:sql2,
parameters:[
{name:'id',value:req.body.id}]};
req.azureMobile.data.execute(query2).then(
function(results2)
{
res.json(results2);
}
);
});
}
};
module.exports=api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment