From 7e7fb2fe08f75ba26b25b5fd6f377bd3300bfbfa Mon Sep 17 00:00:00 2001 From: Hao Liu Date: Mon, 23 Mar 2015 07:35:52 +0800 Subject: [PATCH] past session to locals --- app.js | 7 +++++-- routers/web_router.js | 13 ------------- routes/web_router.js | 18 ++++++++++++++++++ views/about.html | 1 + 4 files changed, 24 insertions(+), 15 deletions(-) delete mode 100644 routers/web_router.js create mode 100644 routes/web_router.js diff --git a/app.js b/app.js index 8bea3de..9903911 100644 --- a/app.js +++ b/app.js @@ -3,9 +3,10 @@ var express = require("express") , path = require('path') , jsforce = require("jsforce") , underscore = require("underscore") - , webRouter = require("./routers/web_router") + , webRouter = require("./routes/web_router") , session = require("express-session") , bodyParser = require("body-parser") + , router = express.Router() , app = express(); app.set("view engine", "html"); @@ -24,7 +25,9 @@ app.use(bodyParser.urlencoded({ extended: true })); -app.use('/', webRouter); +// app.use('/', webRouter); +webRouter(app); +app.use(router); var server = app.listen(process.env.PORT || 3000, function() { var host = server.address().address diff --git a/routers/web_router.js b/routers/web_router.js deleted file mode 100644 index 6612202..0000000 --- a/routers/web_router.js +++ /dev/null @@ -1,13 +0,0 @@ -var express = require("express") - , site = require("../controller/site") - , account = require("../controller/account") - , router = express.Router() - -router.get('/', site.index); -router.post('/login', site.login); -router.get('/about', site.about); -router.get('/oauth/callback', site.callback); -router.get('/account/o', account.list); -router.get('/account/:id', account.view); - -module.exports = router; \ No newline at end of file diff --git a/routes/web_router.js b/routes/web_router.js new file mode 100644 index 0000000..3d52726 --- /dev/null +++ b/routes/web_router.js @@ -0,0 +1,18 @@ +var express = require("express") + , site = require("../controller/site") + , account = require("../controller/account") + , router = express.Router() + +module.exports = function(app) { + app.use(function(req, res, next) { + res.locals.session = req.session; + next(); + }); + + app.get('/', site.index); + app.post('/login', site.login); + app.get('/about', site.about); + app.get('/oauth/callback', site.callback); + app.get('/account/o', account.list); + app.get('/account/:id', account.view); +} \ No newline at end of file diff --git a/views/about.html b/views/about.html index 1e87a87..98fe20c 100644 --- a/views/about.html +++ b/views/about.html @@ -1,5 +1,6 @@ <% include ./template/header.html %>

About

+ <%- JSON.stringify(session) %>
<% include ./template/footer.html %> \ No newline at end of file