Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Complete framework design.

  • Loading branch information...
commit dd6af330afa03cb07e7a1825a15d1651d0eb3fe2 1 parent 99eadb9
Jackson Tian authored

Showing 2 changed files with 95 additions and 27 deletions. Show diff stats Hide diff stats

  1. +7 0 controllers/index.js
  2. +88 27 framework.js
7 controllers/index.js
... ... @@ -0,0 +1,7 @@
  1 +var get = exports.get = {};
  2 +get.index = function () {
  3 + var response = this.response;
  4 + response.setHeader("Content-Type", "text/html");
  5 + response.writeHead("200");
  6 + response.end("<h1>Hello NodeV5.</h1>");
  7 +};
115 framework.js
... ... @@ -1,18 +1,44 @@
1 1 var http = require("http");
2 2 var url = require("url");
  3 +var cookie = require("./cookie");
  4 +var session = require("./session");
  5 +var config = require("./config");
3 6
4   -var Framework = function () {
5   -
  7 +var Context = function (request, response, session) {
  8 + this.request = request;
  9 + this.response = response;
  10 + this.session = session;
6 11 };
7 12
8   -Framework.prototype.handle = function (request, response) {
  13 +var Framework = function () {
  14 + this.sessionManager = new session.SessionManager(config.Timeout);
  15 +};
  16 +Framework.prototype.dispatch = function (request, response) {
9 17 var routeInfo = this.route(request.url);
  18 + var controller;
10 19 try {
11   - var controller = require('../controllers/' + routeInfo.controller).controller;
  20 + controller = require('./controllers/' + routeInfo.controller);
  21 + var method = request.method.toLowerCase() || 'get';
  22 + var action = controller[method] ? controller[method][routeInfo.action] : null;
  23 + if (action) {
  24 + this.enableGet(request, response);
  25 + this.enableCookie(request, response);
  26 + this.enablePost(request, response);
  27 + var curSession = this.enableSession(request, response);
  28 + var context = new Context(request, response, curSession);
  29 + action.apply(context, routeInfo.args);
  30 + } else {
  31 + this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" without action "' + routeInfo.action + '" for "' + request.method + '" request.');
  32 + }
12 33 } catch (ex) {
13   -
  34 + console.log(ex.message);
  35 + console.log(ex.stack);
  36 + this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" dosen\'t exsit.');
14 37 }
15   - // Add get parse supports
  38 +};
  39 +
  40 +// Add get parse supports
  41 +Framework.prototype.enableGet = function (request, response) {
16 42 var _urlMap;
17 43 request.get = function (key) {
18 44 if (!_urlMap) {
@@ -20,8 +46,31 @@ Framework.prototype.handle = function (request, response) {
20 46 }
21 47 return _urlMap.query[key];
22 48 };
  49 +};
  50 +
  51 +// Add cookie parse and set supports
  52 +Framework.prototype.enableCookie = function (request, response) {
  53 + var _cookieMap;
  54 + request.cookie = function (key) {
  55 + if (!_cookieMap) {
  56 + _cookieMap = cookie.parse(request.headers.cookie || "");
  57 + }
  58 + return _cookieMap[key];
  59 + };
  60 + var _setCookieMap = {};
  61 + response.setCookie = function (cookieObj) {
  62 + _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
  63 + var returnVal = [];
  64 + for(var key in _setCookieMap) {
  65 + returnVal.push(_setCookieMap[key]);
  66 + }
23 67
24   - // Add post parse supports
  68 + response.setHeader("Set-Cookie", returnVal.join(", "));
  69 + };
  70 +};
  71 +
  72 +// Add post parse supports
  73 +Framework.prototype.enablePost = function (request, response) {
25 74 if (request.method === "POST") {
26 75 var _postData = "",
27 76 _postMap = "";
@@ -39,30 +88,36 @@ Framework.prototype.handle = function (request, response) {
39 88 };
40 89 });
41 90 }
  91 +};
42 92
43   - // Add cookie parse and set supports
44   - var _cookieMap;
45   - request.cookie = function (key) {
46   - if (!_cookieMap) {
47   - _cookieMap = cookie.parse(request.headers.cookie || "");
48   - }
49   - return _cookieMap[key];
50   - };
51   - var _setCookieMap = {};
52   - response.setCookie = function (cookieObj) {
53   - _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
54   - var returnVal = [];
55   - for(var key in _setCookieMap) {
56   - returnVal.push(_setCookieMap[key]);
  93 +// Add session supports
  94 +Framework.prototype.enableSession = function (request, response) {
  95 + var sessionManager = this.sessionManager;
  96 + var sessionId = request.cookie(session.SESSIONID_KEY);
  97 +
  98 + var curSession;
  99 + if (sessionId && (curSession = sessionManager.get(sessionId))) {
  100 + if (sessionManager.isTimeout(curSession)) {
  101 + sessionManager.remove(sessionId);
  102 + curSession = sessionManager.renew(response);
  103 + } else {
  104 + curSession.updateTime();
57 105 }
  106 + } else {
  107 + curSession = sessionManager.renew(response);
  108 + }
58 109
59   - response.setHeader("Set-Cookie", returnVal.join(", "));
60   - };
  110 + return curSession;
61 111 };
62 112
63   -Framework.prototype.route = function (url) {
64   - // url: /controller/action/parameter1/parameter2
65   - var pathname = url.parse(url).pathname;
  113 +Framework.prototype.handler500 = function (request, response, err) {
  114 + response.writeHead(500, {'Content-Type': 'text/plain'});
  115 + response.end(err);
  116 +};
  117 +
  118 +Framework.prototype.route = function (requestUrl) {
  119 + // /controller/action/parameter1/parameter2
  120 + var pathname = url.parse(requestUrl).pathname;
66 121
67 122 var path = pathname.split("/");
68 123 path.shift(); // Remove the first "/"
@@ -77,7 +132,13 @@ Framework.prototype.route = function (url) {
77 132 var framework = new Framework();
78 133
79 134 var server = http.createServer(function (request, response) {
80   - framework.handle(request, response);
  135 + if (request.url == "/favicon.ico") {
  136 + response.writeHead(404, "Not Found");
  137 + response.end();
  138 + return;
  139 + }
  140 + framework.dispatch(request, response);
81 141 });
82 142
83 143 server.listen(8000);
  144 +console.log("Running at 8000 port.");

0 comments on commit dd6af33

Please sign in to comment.
Something went wrong with that request. Please try again.