From f026c909bdecbec230e105ec4709ae4c08654489 Mon Sep 17 00:00:00 2001 From: jweg <jweg@informatik.uni-kiel.de> Date: Sat, 3 Jun 2017 23:08:16 +0200 Subject: [PATCH] cookies implemented for passing checked files from form --- app.js | 21 ++++++++++++++++----- package.json | 1 + views/home.hbs | 5 ++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index e04f5ea..3b92e43 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,6 @@ var express = require('express'); var bodyParser = require('body-parser'); +var cookieParser = require('cookie-parser'); var exec = require('child_process').exec; @@ -21,28 +22,38 @@ app.use(bodyParser.urlencoded({ })); app.use(bodyParser.json()); app.use(logger("short")); +app.use(cookieParser()); + //set handlebars as view engine app.engine('.hbs', exphbs({extname: '.hbs'})); app.set('view engine', '.hbs'); +//routes app.get('/', function(req, res){ res.render('home', {downloadFiles: downloadNames}); }); -app.post('/waiting', function(req, res){ -console.log("frontend in /waiting", req.body['explorviz-ui-backend']); +app.post('/', function(req, res, next){ var checkedFiles = Object.keys(req.body); -console.log("checkedFiles", checkedFiles); - res.render('download', {test: checkedFiles}); + +if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){ + console.log('files were checked'); + //set cookie to pass checked files for download + res.cookie('fileCookie', checkedFiles).render('download'); +} + else { + console.log('files were not checked'); + res.render('home', {downloadFiles: downloadNames}); + } }); app.get('/download', function(req, res){ -console.log("frontend in /download", req.body); +console.log("checkedFiles in /download", req.cookies.fileCookie); // if (typeof req.body.backend !== 'undefined'){ console.log("inside backend branch"); exec('cd /home/jweg/git/explorviz-ui-backend && mvn compile war:war', (error, stdout, stderr) => { diff --git a/package.json b/package.json index 28ca170..9167364 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "adm-zip": "^0.4.7", "body-parser": "^1.17.1", "child_process": "^1.0.2", + "cookie-parser": "^1.4.3", "express": "^4.15.2", "express-handlebars": "^3.0.0", "fs": "0.0.1-security", diff --git a/views/home.hbs b/views/home.hbs index 5ec6130..c795eef 100644 --- a/views/home.hbs +++ b/views/home.hbs @@ -1,9 +1,8 @@ -<form action="/waiting" method="post"> +<form action="/" method="post"> Choose which files to compile into your custom build of ExplorViz:<br> {{#each downloadFiles as |item|}} <input type="checkbox" name={{item}} value={{item}}>{{item}}<br> {{/each}} <br> - <a > - <button type="submit">Create Build and Download Zip</a> + <button type="submit">Create Build and Download Zip</button> </form> \ No newline at end of file -- GitLab