Exemplo n.º 1
0
  connect.router(function (app) {
    // Process GET's for jsonp
    app.get('/:func', function (req, res) {
      // if ((req.query == null) || (req.query != null && req.query._callback == null)) {
      //   res.writeHead(404);
      //   return res.end();
      // }
      
      var params = {};
      if (req.query != null) {
        if( req.query.params != null) {
          try {
            params = JSON.parse(req.query.params);
            params._callback = req.query._callback;
          } catch (e) {
            // "params" parameter is not a JSON object - may be a coincidence that we got a 
            // standard GET request with a parameter called "params". Just use the req.query 
            // obect as the params
            params = req.query;
          }
        }
        else {
          params = req.query;
        }

        //for js sdk, some jsonp requests will stringiy the request data and send as a query param called _jsonpdata
        //if we see it, parse it as json and send to the request 
        if(req.query._jsonpdata){
          var jsonpdata = null;
          try{
            jsonpdata = JSON.parse(decodeURIComponent(req.query._jsonpdata));
            for(var k in jsonpdata){
              params[k] = jsonpdata[k];
            }
          } catch (e){
            params._jsonpdata = req.query._jsonpdata;
          }
        }
      }
      return callFunction(params, req, res);
    });

    // Process POST's for ajax/web requests
    app.post('/:func', function (req, res) {
      return callFunction(req.body, req, res);
    });

    app['options']('/:func', function(req, res){
      var headers = {'Access-Control-Allow-Origin':'*', 'Access-Control-Allow-Headers':'Origin, X-Request-With, Content-Type', 'Access-Control-Allow-Methods':'POST, GET, OPTIONS', 'Access-Control-Allow-Credentials': 'true'};
      res.writeHead(200, headers);
      res.end("");
    });
    
  })
Exemplo n.º 2
0

var cloud = {
  router : connect().use(connect.bodyParser()).use(
    connect.router(function (app) {
      // For CORS Requests - responds with the headers a browser likes to see so that CORs can function.
      app.options('/:func', cors.optionsResponse);

      // Process GETs, POSTs, and everything else inbetween!
      // Do clever jsonp stuff, and make one unified params object
      app.all('/:func', function (req, res) {
        var params = {};
        params = paramsUtils.normalise(params, req);

        return cloud.callFunction(params, req, res);
      });



      // Handle unspecified functions
      app.all('/', function(req, res){
        res.statusCode = 404;
        res.end(JSON.stringify({error:"Error: no function specified"}));
      });



    })
   ).use(connect.errorHandler({dumpExceptions:true, showMessage:true})), // end router
  /*
   Reaches out to Main.js, and calls the relevant function if it exists.
   Common path for all connect requests, be they POST, GET