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

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