Created
March 4, 2014 06:13
-
-
Save andrewwakeling/9341165 to your computer and use it in GitHub Desktop.
Simple App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "simpleapp", | |
"description": "simple application", | |
"version": "0.0.1", | |
"author": { | |
"name": "Tim Douglas" | |
}, | |
"dependencies": { | |
"express": "3.4.8", | |
"moment": "2.5.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CONFIG = { | |
port: 9000, // Port to listen on | |
cacheUnits: 'seconds', // Cache units, 'days', 'minutes', 'seconds'. See momentjs documentation | |
cacheValue: 10, // Cache value, e.g. 10 (seconds), 5 (days) | |
cachePath: 'cache.json' // Path where cached response will be saved and restored | |
}; | |
var express = require('express') | |
, moment = require('moment') | |
, fs = require('fs'); | |
var cached = {}; | |
var app = express(); | |
/** | |
* Return JSON with a structure which includes the current time. | |
*/ | |
app.get('/api/sample/', function (req, res) { | |
// If there is no cache timestamp set, or we have exceeded the cache duration, get the result. | |
if (!cached.timeStamp || moment().diff(cached.timeStamp, CONFIG.cacheUnits) > CONFIG.cacheValue) { | |
getResults(function (err, results) { | |
cached.results = results; | |
cached.timeStamp = moment(); | |
fs.writeFileSync(CONFIG.cachePath, JSON.stringify(cached)); | |
res.json(200, results); | |
}); | |
} else { | |
// Otherwise, we will used the cached result. | |
process.nextTick(function () { | |
res.json(200, cached.results); | |
}); | |
} | |
}); | |
/** | |
* Gets the results asynchronously. | |
* @param callback - function(err, results) | |
*/ | |
function getResults(callback) { | |
// TODO: Fake a result which includes the current time. | |
var results = { | |
currentTime: moment().toString() | |
} | |
process.nextTick(function () { | |
callback(null, results); | |
}); | |
} | |
/** | |
* Do initialization and invoke the callback once it has finished. | |
* @param callback - function(err) | |
*/ | |
function doInitialization(callback) { | |
if (fs.existsSync(CONFIG.cachePath)) { | |
cached = JSON.parse(fs.readFileSync(CONFIG.cachePath, 'utf8')); | |
} | |
process.nextTick(function () { | |
// TODO: Error handling here. Hardcoding null for no error. | |
callback(null); | |
}); | |
} | |
/** | |
* Do any initialization which is required before the app can begin taking requests. | |
*/ | |
doInitialization(function (err) { | |
if (err) { | |
console.log('Error: ' + err); | |
process.exit(1); | |
} else { | |
console.log('Listening on port: ' + CONFIG.port); | |
app.listen(CONFIG.port); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment