Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add view supports.

  • Loading branch information...
commit 8f970a2ca52dd3e879d416f15aa9ac4f5ad9db12 1 parent dd6af33
@JacksonTian authored
View
16 controllers/index.js
@@ -3,5 +3,19 @@ get.index = function () {
var response = this.response;
response.setHeader("Content-Type", "text/html");
response.writeHead("200");
- response.end("<h1>Hello NodeV5.</h1>");
+ response.end("<h1>Hello NodeV5.</h1>\n");
+};
+get.none = function () {
+ this.none();
+};
+get.json = function () {
+ var obj = {"Hello": "world!"};
+ this.renderJSON(obj);
+};
+get.redirect = function () {
+ this.redirect("https://github.com/JacksonTian/nodev5");
+};
+get.render = function () {
+ var obj = {"title": "NodeV5"};
+ this.renderView("index.html", obj);
};
View
33 footprint.js
@@ -0,0 +1,33 @@
+var footprint = exports;
+// Default template settings, uses ASP/PHP/JSP delimiters, change the
+// following template settings to use alternative delimiters.
+var templateSettings = {
+ evaluate : /<%([\s\S]+?)%>/g,
+ interpolate : /<%=([\s\S]+?)%>/g
+};
+
+footprint.compile = function(str, settings) {
+ var c = settings || templateSettings;
+ var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
+ 'with(obj||{}){__p.push(\'' +
+ str.replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(c.interpolate, function(match, code) {
+ return "'," + code.replace(/\\'/g, "'") + ",'";
+ })
+ .replace(c.evaluate || null, function(match, code) {
+ return "');" + code.replace(/\\'/g, "'")
+ .replace(/[\r\n\t]/g, ' ') + "__p.push('";
+ })
+ .replace(/\r/g, '\\r')
+ .replace(/\n/g, '\\n')
+ .replace(/\t/g, '\\t')
+ + "');}return __p.join('');";
+ return new Function('obj', tmpl);
+};
+
+// Preserves template method for compatible with legacy call.
+footprint.template = function (str, data) {
+ var compilied = footprint.compile(str);
+ return compilied(data);
+};
View
76 framework.js
@@ -3,12 +3,80 @@ var url = require("url");
var cookie = require("./cookie");
var session = require("./session");
var config = require("./config");
+var footprint = require("./footprint");
+var path = require("path");
+var fs = require("fs");
-var Context = function (request, response, session) {
+var Context = function (request, response, session, framework) {
this.request = request;
this.response = response;
this.session = session;
+ this.framework = framework;
};
+Context.prototype.none = function () {
+ this.response.writeHead(204);
+ this.response.end();
+};
+Context.prototype.renderJSON = function (jsonObj) {
+ this.response.setHeader("Content-Type", "application/json");
+ this.response.writeHead(200);
+ this.response.end(JSON.stringify(jsonObj));
+};
+Context.prototype.redirect = function (url) {
+ this.response.setHeader("Location", url);
+ this.response.writeHead(301);
+ this.response.end();
+};
+Context.prototype._renderView = function (viewEngine, template, data) {
+ var framework = this.framework,
+ request = this.request,
+ response = this.response;
+
+ try {
+ response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
+ response.write(viewEngine.template(template, data));
+ response.end();
+ } catch (ex) {
+ console.log(ex.message);
+ console.log(ex.stack);
+ framework.handler500(request, response, "Parse template error.");
+ }
+};
+
+Context.prototype.renderView = function (view, data) {
+ var context = this,
+ framework = context.framework,
+ request = context.request,
+ response = context.response;
+
+ // Get engine.
+ var viewEngine = footprint;
+
+ // Check cache.
+ viewEngine._cache = viewEngine._cache || {};
+ var template = viewEngine._cache[view];
+ if (template) {
+ context._renderView(viewEngine, template, data);
+ } else {
+ var filePath = path.join(__dirname, "views/", view);
+
+ path.exists(filePath, function (exists) {
+ if(!exists) {
+ framework.handler500(request, response, "This template file doesn't exist.");
+ } else {
+ fs.readFile(filePath, "utf8", function(err, file) {
+ if (err) {
+ framework.handler500(request, response, err);
+ } else {
+ viewEngine._cache[view] = file;
+ context._renderView(viewEngine, file, data);
+ }
+ });
+ }
+ });
+ }
+};
+Context.prototype.renderPartial = Context.prototype.renderView;
var Framework = function () {
this.sessionManager = new session.SessionManager(config.Timeout);
@@ -25,8 +93,10 @@ Framework.prototype.dispatch = function (request, response) {
this.enableCookie(request, response);
this.enablePost(request, response);
var curSession = this.enableSession(request, response);
- var context = new Context(request, response, curSession);
- action.apply(context, routeInfo.args);
+ var context = new Context(request, response, curSession, framework);
+ request.on("end", function () {
+ action.apply(context, routeInfo.args);
+ });
} else {
this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" without action "' + routeInfo.action + '" for "' + request.method + '" request.');
}
View
2  mime.js
@@ -17,4 +17,4 @@ exports.types = {
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml"
-};
+};
View
11 views/index.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <title><%=title%></title>
+</head>
+<body>
+<h1><%=title%></h1>
+</body>
+</html>
+
Please sign in to comment.
Something went wrong with that request. Please try again.