@nakedcreativity/cee-ms-node 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +34 -28
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,8 +4,7 @@ const fs = require('fs');
4
4
  const _ = require('underscore');
5
5
  var Liquid = require('nakedcreativity-liquid');
6
6
  var cheerio = require('cheerio');
7
-
8
-
7
+ var bodyParser = require("body-parser");
9
8
 
10
9
  function ceems(config) {
11
10
 
@@ -14,6 +13,7 @@ function ceems(config) {
14
13
  // process config and set defaults
15
14
  // // set up static file hosting
16
15
  this.app.use(express.static(config.assets))
16
+ this.app.use(bodyParser.json());
17
17
 
18
18
  this.engine = Liquid({
19
19
  root: path.resolve('./views/'), // for layouts and partials
@@ -67,36 +67,43 @@ function ceems(config) {
67
67
 
68
68
 
69
69
  this.app.get(/(.*)/, (req, res)=>{
70
- this.auto(req,res);
70
+
71
+ this.render(req,res, page);
72
+
71
73
  })
72
74
 
73
- }
75
+ this.app.post(/draft\/(.*)/, (req, res)=>{
74
76
 
75
- ceems.prototype.auto = function(req, res) {
77
+ // validate authorisation JWT here
78
+ this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
79
+ console.log(req.body.page)
80
+ req.query.edit = true;
81
+ this.render(req,res, req.body.page);
76
82
 
77
- this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
78
- this.pages_map = _.indexBy(this.site.pages, 'url');
83
+ })
79
84
 
80
- var start = Date.now()
85
+ }
81
86
 
82
- if(this.pages_map[req._parsedUrl.pathname]){
83
- var page = this.pages_map[req._parsedUrl.pathname]
87
+ ceems.prototype.render = function(req, res, page=null) {
84
88
 
85
- if(page.redirect){
86
- res.redirect(parseInt(page.redirect.type), page.redirect.to)
87
- }
89
+ if(!page){
90
+ this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
91
+ this.pages_map = _.indexBy(this.site.pages, 'url');
88
92
 
89
- var page = JSON.parse(fs.readFileSync('./pages/'+page.handle+'.json', 'utf8'));
93
+ if(this.pages_map[req._parsedUrl.pathname]){
94
+ var page = this.pages_map[req._parsedUrl.pathname]
90
95
 
91
- }else{
92
- try {
93
- var page = JSON.parse(fs.readFileSync('./drafts/'+req._parsedUrl.pathname.replace('/drafts/','')+'.json', 'utf8'));
94
- }catch (error){
96
+ if(page.redirect){
97
+ res.redirect(parseInt(page.redirect.type), page.redirect.to)
98
+ }
99
+
100
+ var page = JSON.parse(fs.readFileSync('./pages/'+page.handle+'.json', 'utf8'));
101
+
102
+ }else{
95
103
  res.status(404).send('Sorry, this page does not exist');
96
104
  }
97
105
  }
98
106
 
99
-
100
107
  // get any external data, and save this into the page
101
108
  _.each(page.external, function(ext, key){
102
109
 
@@ -135,7 +142,10 @@ ceems.prototype.auto = function(req, res) {
135
142
  var $ = cheerio.load(html);
136
143
 
137
144
  $.root().children().attr('data-module-type', module.type);
138
- $.root().children().attr('data-module-id', module.id);
145
+ if(module._id){
146
+ $.root().children().attr('data-module-id', module._id);
147
+ }
148
+
139
149
 
140
150
  html = $.html()
141
151
  }
@@ -168,7 +178,7 @@ ceems.prototype.auto = function(req, res) {
168
178
  if(req.query.edit != null){
169
179
  var $ = cheerio.load(html);
170
180
  $.root().children().attr('data-module-type', module.type);
171
- $.root().children().attr('data-module-id', module.id);
181
+ $.root().children().attr('data-module-id', module._id);
172
182
  //console.log($.root().children())
173
183
 
174
184
  html = $.html()
@@ -196,10 +206,8 @@ ceems.prototype.auto = function(req, res) {
196
206
  if(err){
197
207
  console.log(err)
198
208
  }
199
- var finish = Date.now()
200
- console.log(finish - start, 'render time')
201
- console.log('..................')
202
- res.header('X-FRAME-OPTIONS', 'ALLOW-FROM localhost:3006');
209
+ //res.header('X-FRAME-OPTIONS', 'allow-from http://localhost:3002');
210
+ //console.log(res)
203
211
  res.send(html)
204
212
  })
205
213
 
@@ -214,13 +222,11 @@ ceems.prototype.auto = function(req, res) {
214
222
  ceems.prototype.init = function(){
215
223
 
216
224
  // needs to be dealt with on each request, or results cache in someway
217
-
218
-
219
225
 
220
226
  }
221
227
 
222
228
  ceems.prototype.viewsPath = function(){
223
229
  return './views/'
224
- }
230
+ }
225
231
 
226
232
  module.exports = ceems;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakedcreativity/cee-ms-node",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Cee.ms for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {