Posts Tagged ‘node.acs’

Node.ACS Tutorial 1: PixGrid photo uploader

Friday, November 30th, 2012

After launching the Node.ACS developer preview 2 last week, we are excited to share with you some real examples of how to write a kickass Node.ACS app that connects to ACS as the data backend. We’ll be sharing several more examples in the coming weeks .

Today I want to walk through a sample Node.ACS app called PixGrid which lets users signup, login, upload and view photos from a web browser. It uses ACS as the data backend to store user data and photos. You can download the source code from Github.

PixGrid is built on Node.ACS’s easy to use MVC Framework. In this app, the M(model) is ACS. We want to let ACS handle all the data storage and only focus on the application logic and view in the Node.ACS part.

config.json

This is one of the entry points of Node.ACS MVC framework projects. This file defines all the routing and filtering logic for the app.

PixGrid Routes

  • Signup
    • { “path”: “/signup”, “method”: “get”, “callback”: “application#signup” }
      This method renders the signup page
    • { “path”: “/signup”, “method”: “post”, “callback”: “user#signup” }
      This method performs the actual signup with ACS
  • Login
    • { “path”: “/login”, “method”: “POST”, “callback”: “session#login” }
      This method performs actual login action with ACS
    • { “path”: “/login”, “method”: “GET”, “callback”: “application#login” }
      This method renders the login page
  • Logout
    • { “path”: “/logout”, “method”: “get”, “callback”: “session#logout” }
      This method performs a call to ACS to logout a user
  • Photo view (query)
    •  { “path”: “/”, “method”: “GET”, “callback”: “application#index” }
      Display login page if a user is not logged in, otherwise, display photos uploaded by the user
  • Photo create
    • { “path”: “/photos/create”, “method”: “post”, “callback”: “photos#_create” }
      Upload a photo to ACS by selecting a photo file
    • { “path”: “/photos/create.json”, “method”: “post”, “callback”: “photos#_create_json” }
      Upload a photo to ACS by ajax drag and drop (only works on Chrome and Firefox)
  • Photo delete
    •  { “path”: “/photos/:id/delete”, “method”: “get”, “callback”: “photos#_destroy” }
      Delete a photo from ACS
  • Page not found
    • { “path”: “*”, “method”: “get”, “callback”: “application#page_not_found” }
      Render a page not found for everything else

 

PixGrid Filter

In PixGrid, most pages require a user to be logged in, so we put a checkUserSession filter to all pages

  • { “path”: “/”, “callback”: “session_filter#checkUserSession” }
    Inside the checkUserSession method, it bypasses /login, /logout and /signup

app.js

app.js is another entry point of Node.ACS MVC framework projects. It initializes the ACS and configures express, which is included in the MVC project by default. Please make sure to update the following line in app.js to fill in your own ACS oauth key and secret.

ACS.init('OAUTH_KEY', 'OAUTH_SECRET_KEY');

ACS user sessions

When writing apps for devices,  it is common to keep user specific information on the device and use it later because only one user can use your app at a time, however, the same code will not work well if it is running on a server because the code would be shared by multiple users.

Use photo upload as an example. If you save an uploaded file to a temp file on your Node.ACS server before sending it to ACS, you need to make sure the filename is unique for each transaction so they won’t get overwritten. For example, if user A uploads a photo to your Node.ACS server, your code stores it as photo.tmp, before the server gets a chance to send this photo to ACS, user B uploads another photo and your code will overwrite user A’s photo.tmp if you choose  to use the same tmp filename.  Check out photo.js method _create_json to see how it handles a file drag and dropped by AJAX calls.

The same goes to ACS user sessions, it is tempting to store an ACS user session on Node.ACS server for subsequent calls. But this session will get overwritten when another user logs in to ACS through the same Node.ACS server. That’s why ACS module for Node.ACS provides additional parameters (req,res) to let you pass through a user session cookie without the need to store it on the server.

ACS.Users.login({
    login: req.body.un,
    password: req.body.pw
  }, function(data) {
     // handling login callback
  }, req, res);
}

It is important to pass the req and res to ACS library so that it will pass along or set the cookies if needed. In the login case, it will set the ACS server returned cookie in res which will be passed back to your device. Most device handles cookies automatically so you don’t need to do anything extra on the device.

Subsequently, the photo upload call will take the cookies stored passed along by the device to ACS.

ACS.Photos.create(data, function(e) {
    if(e.success && e.success === true){
      req.session.flash = {msg:"Successfully create a photo #"+e.photos[0].id, r:0};
      res.redirect('/');
    }else{
      req.session.flash = {msg:e.message, r:0};
      res.redirect('/');
    }
  }, req, res);

As long as you keep this key difference in mind, you can use almost the same code that communicates with ACS in both Titanium and Node.ACS.

Please use Node.ACS google group if you have any questions.

Node.js for ACS Public Developer Preview

Tuesday, September 4th, 2012

Bring on the Node.js code! For the past few weeks we’ve been running an internal developer preview of Node.js hosting on Appcelerator Cloud Services, which we call Node.ACS. This allows developers to code server side apps in familiar JavaScript, then deploy them into the ACS cloud. Just like with the rest of the ACS features, we’ll take care of all of the deployment and hosting issues.

Now we’re happy to announce that we’re expanding the developer preview to the first 100 developers who send an email to me at mgoff@appcelerator.com to request access. You’ll get first crack at trying out our Node.js hosting solution that will be closely integrated with ACS and Titanium Studio. Help us to develop the next great feature for ACS! The ACS Team is looking for feedback about any unknown problems encountered, along with suggestions for additional features which should be added.

Documentation about how to use Node.ACS is available at http://cloud.appcelerator.com/docs/nodejs/custom_code_tutorial. For support, post a question to the Appcelerator Community Q&A. Be sure to use the node.acs tag when posting so that we can see it right away. To view existing Q&A about Node.ACS, view all questions with the node.acs tag.

Here are the features we support:

  • Create, test, and publish a Node.js app
  • Login/logout using Appcelerator developer accounts
  • Create a new node.js app
  • Run the node.js app locally for testing
  • Publish the node.js app to the ACS cloud
  • List your currently running apps
  • Retrieve log output from your app
  • Unpublish an app
  • Connect with all ACS features through an included node.js library
 

Being a developer preview, there are some caveats:

  • We’re limiting this part of the preview to 100 users, but will expand that number later on.
  • You can publish multiple versions of your app, but only the latest version will be accessible. If you unpublish the latest version, the previous version will become accessible.
  • We will periodically unpublish all running apps to keep our resource usage under control during this free developer preview. Therefore if you see a published app not running anymore, just try publishing it again. The same URL will be used when your app is republished.
  • If a published app cannot be started (dependencies missing from package.json, etc.) we do not currently provide any warning. You’ll just see a 503 error when trying to access the given URL for your app. Be sure to test locally before publishing your app to ACS.
  • Published apps can take up to two minutes to go live. This is due to the startup time required to launch a new EC2 node on Amazon.
  • Your apps may run slower than expected at times. We are using t1.micro Amazon EC2 nodes during the initial developer preview.
 

Here are some upcoming features under development which will be released in a later version of the developer preview:

  • Websockets support
  • Immediate availability of apps when published
  • Deployment of apps to larger, shared Amazon EC2 nodes
  • Vanity CNAME support
  • SSL certificate upload support
  • Generation of client-side bindings to use in Titanium Studio projects
  • Creation of standalone node.js projects from Titanium Studio
  • Creation of combined client & server projects from Titanium Studio