Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Complete framework design.

  • Loading branch information...
commit dd6af330afa03cb07e7a1825a15d1651d0eb3fe2 1 parent 99eadb9
@JacksonTian authored
Showing with 95 additions and 27 deletions.
  1. +7 −0 controllers/index.js
  2. +88 −27 framework.js
View
7 controllers/index.js
@@ -0,0 +1,7 @@
+var get = exports.get = {};
+get.index = function () {
+ var response = this.response;
+ response.setHeader("Content-Type", "text/html");
+ response.writeHead("200");
+ response.end("<h1>Hello NodeV5.</h1>");
+};
View
115 framework.js
@@ -1,18 +1,44 @@
var http = require("http");
var url = require("url");
+var cookie = require("./cookie");
+var session = require("./session");
+var config = require("./config");
-var Framework = function () {
-
+var Context = function (request, response, session) {
+ this.request = request;
+ this.response = response;
+ this.session = session;
};
-Framework.prototype.handle = function (request, response) {
+var Framework = function () {
+ this.sessionManager = new session.SessionManager(config.Timeout);
+};
+Framework.prototype.dispatch = function (request, response) {
var routeInfo = this.route(request.url);
+ var controller;
try {
- var controller = require('../controllers/' + routeInfo.controller).controller;
+ controller = require('./controllers/' + routeInfo.controller);
+ var method = request.method.toLowerCase() || 'get';
+ var action = controller[method] ? controller[method][routeInfo.action] : null;
+ if (action) {
+ this.enableGet(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);
+ } else {
+ this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" without action "' + routeInfo.action + '" for "' + request.method + '" request.');
+ }
} catch (ex) {
-
+ console.log(ex.message);
+ console.log(ex.stack);
+ this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" dosen\'t exsit.');
}
- // Add get parse supports
+};
+
+// Add get parse supports
+Framework.prototype.enableGet = function (request, response) {
var _urlMap;
request.get = function (key) {
if (!_urlMap) {
@@ -20,8 +46,31 @@ Framework.prototype.handle = function (request, response) {
}
return _urlMap.query[key];
};
+};
+
+// Add cookie parse and set supports
+Framework.prototype.enableCookie = function (request, response) {
+ var _cookieMap;
+ request.cookie = function (key) {
+ if (!_cookieMap) {
+ _cookieMap = cookie.parse(request.headers.cookie || "");
+ }
+ return _cookieMap[key];
+ };
+ var _setCookieMap = {};
+ response.setCookie = function (cookieObj) {
+ _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
+ var returnVal = [];
+ for(var key in _setCookieMap) {
+ returnVal.push(_setCookieMap[key]);
+ }
- // Add post parse supports
+ response.setHeader("Set-Cookie", returnVal.join(", "));
+ };
+};
+
+// Add post parse supports
+Framework.prototype.enablePost = function (request, response) {
if (request.method === "POST") {
var _postData = "",
_postMap = "";
@@ -39,30 +88,36 @@ Framework.prototype.handle = function (request, response) {
};
});
}
+};
- // Add cookie parse and set supports
- var _cookieMap;
- request.cookie = function (key) {
- if (!_cookieMap) {
- _cookieMap = cookie.parse(request.headers.cookie || "");
- }
- return _cookieMap[key];
- };
- var _setCookieMap = {};
- response.setCookie = function (cookieObj) {
- _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
- var returnVal = [];
- for(var key in _setCookieMap) {
- returnVal.push(_setCookieMap[key]);
+// Add session supports
+Framework.prototype.enableSession = function (request, response) {
+ var sessionManager = this.sessionManager;
+ var sessionId = request.cookie(session.SESSIONID_KEY);
+
+ var curSession;
+ if (sessionId && (curSession = sessionManager.get(sessionId))) {
+ if (sessionManager.isTimeout(curSession)) {
+ sessionManager.remove(sessionId);
+ curSession = sessionManager.renew(response);
+ } else {
+ curSession.updateTime();
}
+ } else {
+ curSession = sessionManager.renew(response);
+ }
- response.setHeader("Set-Cookie", returnVal.join(", "));
- };
+ return curSession;
};
-Framework.prototype.route = function (url) {
- // url: /controller/action/parameter1/parameter2
- var pathname = url.parse(url).pathname;
+Framework.prototype.handler500 = function (request, response, err) {
+ response.writeHead(500, {'Content-Type': 'text/plain'});
+ response.end(err);
+};
+
+Framework.prototype.route = function (requestUrl) {
+ // /controller/action/parameter1/parameter2
+ var pathname = url.parse(requestUrl).pathname;
var path = pathname.split("/");
path.shift(); // Remove the first "/"
@@ -77,7 +132,13 @@ Framework.prototype.route = function (url) {
var framework = new Framework();
var server = http.createServer(function (request, response) {
- framework.handle(request, response);
+ if (request.url == "/favicon.ico") {
+ response.writeHead(404, "Not Found");
+ response.end();
+ return;
+ }
+ framework.dispatch(request, response);
});
server.listen(8000);
+console.log("Running at 8000 port.");
Please sign in to comment.
Something went wrong with that request. Please try again.