Example #1
0
app.post("/", function (env, callback) {
    var req = new strata.Request(env);

    req.params(function (err, params) {
        if (err && strata.handleError(err, env, callback)) {
            return;
        }

        var photo = params.photo;

        // Do some simple validation.
        if (!photo) {
            callback(403, {}, 'Param "photo" is required');
        } else if (!(photo instanceof multipart.File)) {
            callback(403, {}, 'Param "photo" must be a file upload');
        } else if (!(/image\//).test(photo.type)) {
            callback(403, {}, 'Param "photo" must be an image');
        } else {
            var content = "";

            content += "The photo was uploaded successfully. Here are its properties:";
            content += "<ul>";

            ["path", "name", "type", "size"].forEach(function (prop) {
                content += "<li>" + prop + ": " + photo[prop] + "</li>";
            });

            content += "</ul>";

            callback(200, {}, content);
        }
    });
});
Example #2
0
routes.upload = function( env, callback ) {
  var
    req    = new strata.Request(env),
    upload = new Upload( env, callback );

  upload.done = function( res, upload ) {
    if ( 200 !== res.statusCode )
      return this.callback( res.statusCode, {}, res.body );

    var
      content = {},
      self    = this;

    ["path", "name", "type", "size"].forEach(function( prop ) {
      content[prop] = self.file[prop];
    });

    content.url = upload.url;

    this.callback(200, {}, tmpl.upload( content ) );
  };

  req.params(function( err, params ) {
    upload.handler( err, params );
  });
};
app.get("/", function (env, callback) {
    var req = new strata.Request(env);

    if (req.accept("text/html")) {
        callback(200, {"Content-Type": "text/html"}, "<p>You accept HTML!</p>");
    } else {
        callback(200, {"Content-Type": "text/plain"}, "You don't accept HTML. :(");
    }
});
Example #4
0
strata.post("/users", function (env, callback) {
  var req = new strata.Request(env);

  req.params(function (err, params) {
    if (err && strata.handleError(err, env, callback)) {
      return;
    }

    if (params.username) {
      users.push(params.username);
    }

    // Redirect to /users.
    redirect(env, callback, "/users");
  });
});
strata.post("/users", function (env, callback) {
  var req = new strata.Request(env);

  req.params(function (err, params) {
    if (err && strata.handleError(err, env, callback)) {
      return;
    }

    // Weak validation, but sufficient for the example.
    if (params.first_name && params.last_name && params.username) {
      var id = userId++;

      users[id] = {
        id: id,
        firstName: params.first_name,
        lastName: params.last_name,
        username: params.username
      };
    }

    // Redirect to /users.
    redirect(env, callback, "/users");
  });
});