Freigeben über


New Startup Scripts Preview Feature in Azure Mobile Services

Azure Mobile Services today allows you to rapidly build mobile apps using table scripts and custom APIs for server side functionality. However for certain advance scenarios it is often required to hook into the core of the mobile service backend to handle and respond to events that is otherwise not possible. To make such scenarios possible, we’ve introduced the Extensions framework. Extensions is a new preview feature where by you should be able to write code that we’ll invoke throughout life cycle of your application. To install an extension you need to have Source Control enabled and drop a script with pre-defined name into the ‘extensions’ folder inside ‘service’ folder.

First extension point that we’re introducing is called ‘Startup’ script. This is a script file with name ‘startup.js’ that exports a method called ‘startup’. We invoke the startup method when your application starts. Your application starts on the first request after it is created, restarted or has been idle for about half an hour. Startup method has following signature:

 

 exports.startup = function(context, done) { 
 // write your logic here 
 done(); 
};

 

Here context is an object that exposes certain key members that would allow you to write interesting startup scripts. The members are as follows:

  • app: This is the expressjs app object. This object gives you complete control over your application. You can write custom routes, middleware or even use socket.io to do realtime communication.
  • tables, config, etc.: All the services that you get from request.service in custom api, are also exposed directly from the context.

With access to the expressjs app object, you are only limited by your imagination in terms of what you can build with your mobile service. We’re really excited to see what you do with startup scripts. Give them a try and share your comments and suggestions with us.

Comments

  • Anonymous
    January 14, 2014
    Very interesting.  Nice to confirm that it is express.js behind the scenes with mobile services
  • Anonymous
    January 30, 2014
    If we load custom routes here, are we before or after the static handler? If we are after, this could cause an issue with the server under load (disk reads on every hit).
  • Anonymous
    February 17, 2014
    I don't know how to make even the simplest startup script work. E.g. the following logs 'Starting new...' to the console - but the happy smily is never logged. Can anybody tell me why?exports.startup = function(context, done) {
    console.log('Starting new...');context.app.use(function(req, res, next) {    console.log(':-)');    next();});done();
    };If I ever get this working, where can I get the userId for the current user in the middleware?
  • Anonymous
    February 21, 2014
    NielsLBeck and RobYour startup script is executed after all routes have been registered. To enable late addition of middleware please read the following article:www.exratione.com/.../nodejs-abusing-express-3-to-enable-late-addition-of-middleware
  • Anonymous
    May 08, 2014
    Any more information how to use this feature, as I also can't get it working
  • Anonymous
    May 21, 2014
    sam can you please provide more detail? What is that you're trying to do and what problem are you facing?Thanks
  • Anonymous
    July 20, 2014
    Hello Hasan,I am very keen to see an example how we could use Socket.IO in this startup method? Seems extremely powerful.