Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var formidable = require("formidable");
  2. function templatify(str, args) {
  3. if (args == undefined)
  4. return str;
  5. for (var i in args) {
  6. str = str.split("{{"+i+"}}").join(args[i]);
  7. }
  8. return str;
  9. }
  10. module.exports = function(options) {
  11. this.req = options.req;
  12. this.res = options.res;
  13. this.templates = options.templates;
  14. this.views = options.views;
  15. this.conf = options.conf;
  16. }
  17. module.exports.prototype = {
  18. end: function(str) {
  19. this.res.end(str);
  20. },
  21. succeed: function(obj) {
  22. obj = obj || {};
  23. obj.success = true;
  24. this.end(JSON.stringify(obj));
  25. },
  26. fail: function(err) {
  27. obj = obj || {};
  28. obj.success = false;
  29. obj.error = error;
  30. this.end(JSON.stringify(obj));
  31. },
  32. template: function(name, args) {
  33. var str = this.templates[name];
  34. if (!str)
  35. throw new Error("No such template: "+name);
  36. return templatify(str, args);
  37. },
  38. view: function(name, args) {
  39. var str = this.views[name];
  40. if (!str)
  41. throw new Error("No such view: "+name);
  42. return templatify(str, args);
  43. },
  44. getPostData: function(cb) {
  45. if (this.req.method.toUpperCase() != "POST")
  46. return cb(new Error("Expected POST request, got "+this.req.method));
  47. var form = new formidable.IncomingForm();
  48. form.parse(this.req, cb);
  49. }
  50. }