From 40904a7f096d2827c04b6dfb816306c0e053062b Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 4 Apr 2016 09:59:37 +0100 Subject: [PATCH] added session --- .gitignore | 1 + obrand-server.js | 9 +++++-- package.json | 1 + server/.jshintrc | 4 ---- server/app/pages.js | 52 ++++++++++++++++++++++++++-------------- server/units/db-pages.js | 29 ++++++++++++++++++++++ 6 files changed, 72 insertions(+), 24 deletions(-) delete mode 100644 server/.jshintrc diff --git a/.gitignore b/.gitignore index eb51e84..0854e72 100644 --- a/.gitignore +++ b/.gitignore @@ -191,3 +191,4 @@ testem.log /.cache /.gradle /server/static/* +/wwwroot/assets diff --git a/obrand-server.js b/obrand-server.js index bf10ec2..a8da35d 100644 --- a/obrand-server.js +++ b/obrand-server.js @@ -6,7 +6,7 @@ * */ var express = require('express'), path = require('path'), http = require('http'); - +var session = require('session'); var morgan = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); @@ -22,9 +22,14 @@ app.set('view engine', 'jade'); app.use(morgan('dev')); app.use(bodyParser.json()); app.use(bodyParser.json({type: 'application/vnd.api+json'})); -app.use(cookieParser()); +app.use(cookieParser('!n87klqX39cB:7ayiRzEL5yRy5y938')); +app.use(session({ +secret: 'G)+W&2W5C3V6gXJ.8mSD!l/-n3D]EV', resave: false, +saveUninitialized: false +})); app.use('/export',express.static(path.join(__dirname,'server/static'))); app.use('/css',express.static(path.join(__dirname,'server/css'))); +app.use('/',express.static(path.join(__dirname,'wwwroot'))); console.log(path.join(__dirname,'server/static')); diff --git a/package.json b/package.json index a9e70b7..3f195d5 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "devDependencies": { "log4js": "^0.6.33", + "session": "^0.1.0", "util": "^0.10.3" } } diff --git a/server/.jshintrc b/server/.jshintrc deleted file mode 100644 index 59f01b9..0000000 --- a/server/.jshintrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "node": true, - "esnext": true -} diff --git a/server/app/pages.js b/server/app/pages.js index fcd852c..25169ab 100644 --- a/server/app/pages.js +++ b/server/app/pages.js @@ -38,28 +38,46 @@ var doRenderPage = (page) => { console.log('- doRenderPage'); }; - eventHandler.on('renderPage', eventRenderPage); - - module.exports = function(app) { var express = require('express'); var pagesRouter = express.Router(); pagesRouter.get('/', function(req, res) { - dbPages.getPageList() - .then(function(data) { - console.log(util.inspect(data)); - res.render('pages', {data:data}); - }).catch((err)=> { - console.error(err); - res.status(401).end(); - }); + console.log(req.query.filter); + dbPages.getFullPageList() + .then(function(data) { + // console.log(util.inspect(data)); + + const response = { + data: data + }; + + res.status(200).send(response); + + }).catch((err)=> { + console.error(err); + res.status(401).end(); + }); }); + pagesRouter.get('/list', function(req, res) { + dbPages.getPageList() + .then(function(data) { + console.log(util.inspect(data)); + + res.render('pages', {data:data}); + + }).catch((err)=> { + console.error(err); + res.status(401).end(); + }); + }); + + pagesRouter.post('/', function(req, res) { console.log(util.inspect(req.body.data)); @@ -70,10 +88,9 @@ module.exports = function(app) { data: data }; - console.log('Finalising...'); res.status(200).send(response); - eventHandler.emit('renderPage',data.attributes); + eventHandler.emit('renderPage', data.attributes); }) .catch((err)=> { @@ -82,7 +99,7 @@ module.exports = function(app) { }); }); -pagesRouter.patch('/', function(req, res) { + pagesRouter.patch('/', function(req, res) { console.log(util.inspect(req.body.data)); dbPages.addNewPage(req.body.data) @@ -94,7 +111,7 @@ pagesRouter.patch('/', function(req, res) { console.log('Finalising...'); res.status(200).send(response); - eventHandler.emit('renderPage',data.attributes); + eventHandler.emit('renderPage', data.attributes); }) .catch((err)=> { @@ -103,8 +120,6 @@ pagesRouter.patch('/', function(req, res) { }); }); - - pagesRouter.get('/:id', function(req, res) { res.send({ pages: { @@ -130,7 +145,8 @@ pagesRouter.patch('/', function(req, res) { // To use req.body, run: // Npm install --save-dev body-parser - app.use('/api/pages', require('body-parser').json({type: 'application/vnd.api+json'})); + app.use('/api/pages', + require('body-parser').json({type: 'application/vnd.api+json'})); // After installing, you need to `use` the body-parser for // this mock uncommenting the following line: diff --git a/server/units/db-pages.js b/server/units/db-pages.js index d31227e..1dfed6a 100644 --- a/server/units/db-pages.js +++ b/server/units/db-pages.js @@ -31,6 +31,35 @@ module.exports = function(db) { }); }; + module.getFullPageList = function() { + return new Promise(function(resolve, reject) { + + db.any("select * from pages;") + .then((d) => { + let rArray = []; + for (var l = 0;l < d.length;l++) { + let attributeObj = $U.newObjectFrom(d[l],['id','cid','vid','pid','content','title']); + attributeObj = $U.populateObject(d[l].data,attributeObj); + + let newObj = {"id":attributeObj.id, "type":"page","attributes":attributeObj}; + + // { data: [ { "id": "56b7ecbc1ef21172377d6159", "type": "guest", "attributes": { "name":"Ray","message":"First ever guest post entry" } }, ... ] } + + + rArray.push(newObj); + } + return resolve(rArray); + + + }) + .catch((err)=> { + console.log('+getPageList failed'); + console.log(err); + return reject(err); + }); + }); + }; + module.sqlInsertPage = function(data) { return new Promise(function(resolve, reject) { db.func('upsert_page',data)