added session

This commit is contained in:
Martin Donnelly 2016-04-04 09:59:37 +01:00
parent 5418093482
commit 40904a7f09
6 changed files with 72 additions and 24 deletions

1
.gitignore vendored
View File

@ -191,3 +191,4 @@ testem.log
/.cache /.cache
/.gradle /.gradle
/server/static/* /server/static/*
/wwwroot/assets

View File

@ -6,7 +6,7 @@
* *
*/ */
var express = require('express'), path = require('path'), http = require('http'); var express = require('express'), path = require('path'), http = require('http');
var session = require('session');
var morgan = require('morgan'); var morgan = require('morgan');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
@ -22,9 +22,14 @@ app.set('view engine', 'jade');
app.use(morgan('dev')); app.use(morgan('dev'));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+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('/export',express.static(path.join(__dirname,'server/static')));
app.use('/css',express.static(path.join(__dirname,'server/css'))); 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')); console.log(path.join(__dirname,'server/static'));

View File

@ -31,6 +31,7 @@
}, },
"devDependencies": { "devDependencies": {
"log4js": "^0.6.33", "log4js": "^0.6.33",
"session": "^0.1.0",
"util": "^0.10.3" "util": "^0.10.3"
} }
} }

View File

@ -1,4 +0,0 @@
{
"node": true,
"esnext": true
}

View File

@ -38,28 +38,46 @@ var doRenderPage = (page) => {
console.log('- doRenderPage'); console.log('- doRenderPage');
}; };
eventHandler.on('renderPage', eventRenderPage); eventHandler.on('renderPage', eventRenderPage);
module.exports = function(app) { module.exports = function(app) {
var express = require('express'); var express = require('express');
var pagesRouter = express.Router(); var pagesRouter = express.Router();
pagesRouter.get('/', function(req, res) { pagesRouter.get('/', function(req, res) {
dbPages.getPageList() console.log(req.query.filter);
.then(function(data) {
console.log(util.inspect(data));
res.render('pages', {data:data});
}).catch((err)=> {
console.error(err);
res.status(401).end();
});
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) { pagesRouter.post('/', function(req, res) {
console.log(util.inspect(req.body.data)); console.log(util.inspect(req.body.data));
@ -70,10 +88,9 @@ module.exports = function(app) {
data: data data: data
}; };
console.log('Finalising...'); console.log('Finalising...');
res.status(200).send(response); res.status(200).send(response);
eventHandler.emit('renderPage',data.attributes); eventHandler.emit('renderPage', data.attributes);
}) })
.catch((err)=> { .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)); console.log(util.inspect(req.body.data));
dbPages.addNewPage(req.body.data) dbPages.addNewPage(req.body.data)
@ -94,7 +111,7 @@ pagesRouter.patch('/', function(req, res) {
console.log('Finalising...'); console.log('Finalising...');
res.status(200).send(response); res.status(200).send(response);
eventHandler.emit('renderPage',data.attributes); eventHandler.emit('renderPage', data.attributes);
}) })
.catch((err)=> { .catch((err)=> {
@ -103,8 +120,6 @@ pagesRouter.patch('/', function(req, res) {
}); });
}); });
pagesRouter.get('/:id', function(req, res) { pagesRouter.get('/:id', function(req, res) {
res.send({ res.send({
pages: { pages: {
@ -130,7 +145,8 @@ pagesRouter.patch('/', function(req, res) {
// To use req.body, run: // To use req.body, run:
// Npm install --save-dev body-parser // 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 // After installing, you need to `use` the body-parser for
// this mock uncommenting the following line: // this mock uncommenting the following line:

View File

@ -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) { module.sqlInsertPage = function(data) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
db.func('upsert_page',data) db.func('upsert_page',data)