Skip to content

Instantly share code, notes, and snippets.

@sidonaldson
Created May 16, 2016 16:28
Show Gist options
  • Save sidonaldson/f5811cf39fb798e344e1ddc89f36035d to your computer and use it in GitHub Desktop.
Save sidonaldson/f5811cf39fb798e344e1ddc89f36035d to your computer and use it in GitHub Desktop.
Node ExpressJS Contenful.js basic sync middleware example and routes
'use strict'
var contentful = require('contentful'),
util = require('util'),
async = require('async'),
fs = require('fs'),
_ = require('lodash'),
client = contentful.createClient({
space: 'ctupzx1pv75n',
accessToken: '601c31d2ec144e4d9e2dd4bd9771fef57ccf88d64ea722bca9ba2a749104d8a6'
}),
data = {},
nextSyncToken = null,
fileName = 'contentful.json',
localCache = './app/data/'
function initialSync(obj) {
data = obj
nextSyncToken = obj.nextSyncToken
saveToLocal(obj)
console.log('Contentful: saved initial data')
}
function mergeData(obj) {
data = _.merge(data, obj)
nextSyncToken = obj.nextSyncToken
saveToLocal(obj)
console.log('Contentful: parsed new data')
}
function fetchLocal() {
console.log('Contentful: fetching local data')
fs.readFile(localCache + fileName, 'utf8', function(err, src) {
data = JSON.parse(src)
nextSyncToken = data.nextSyncToken
console.log('Contentful: fetched local data')
})
}
function fetchRemote(firstLoad) {
console.log('Contentful: fetching remote data', nextSyncToken)
var syncOptions = {
resolveLinks: false
}
if (firstLoad) syncOptions.initial = true
else syncOptions.nextSyncToken = nextSyncToken
client.sync(syncOptions).then(function(result) {
if (nextSyncToken === null) initialSync(result)
else mergeData(result)
}, function(err) {
console.log('Contentful: error loading remote data', err)
})
}
function saveToLocal(obj) {
var newData = JSON.stringify(obj, null, 2)
if (newData.length) fs.writeFile(localCache + fileName, newData)
}
module.exports = {
init: function() {
fs.exists(localCache + fileName, function(exists) {
if (exists) {
fetchLocal()
} else {
fetchRemote(true)
}
})
console.log('Contentful: Initial dump of data.')
},
update: function(req, res, next) {
fetchRemote()
res.end()
},
pluck: function(name) {
console.log('Contentful: returning data: ' + name)
return data[name] || {}
}
}
'use strict';
var fs = require('fs'),
http = require('http'),
https = require('https'),
express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
favicon = require('serve-favicon'),
Contentful = require('./middlewares/contentful-cache').init()
module.exports = function() {
// Initialize express app
var app = express();
// uncomment after placing your favicon in /public
app.use(favicon('public/favicon/favicon.ico'));
// Set views path and view engine
app.engine('server.view.html', 'swig');
app.set('view engine', 'server.view.html');
app.set('views', './app/views');
// Request body parsing middleware should be above methodOverride
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
// Init the routes
require('./routes')(app);
//500
app.use(function(err, req, res, next) {
if (!err) return next();
console.error(err.stack);
res.status(500).render('500', {
error: (process.env.NODE_ENV === 'development') ? err.stack : err.message
});
});
//404
app.use(function(req, res) {
res.status(404).render('404', {
url: req.originalUrl,
error: 'Not Found'
});
});
return app;
};
'use strict';
var _ = require('lodash'),
Contentful = require('./middlewares/contentful-cache'),
marked = require('marked');
module.exports = function(app) {
app.all('/contentful-incoming', Contentful.update);
app.get('/contentful', function(req, res) {
res.render('contentful',{
localtitle: 'Contentful - ',
data: Contentful.pluck('entries')
});
});
app.get('/contentful/:index', function(req, res) {
res.render('contentful-article',{
localtitle: 'Contentful - ',
data: Contentful.pluck('entries'),
index: req.params.index,
marked: marked
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment