Skip to content

Instantly share code, notes, and snippets.

@hontas
Created January 26, 2014 02:18
Show Gist options
  • Save hontas/8627227 to your computer and use it in GitHub Desktop.
Save hontas/8627227 to your computer and use it in GitHub Desktop.
Express server for fakeEnd
var express = require('express'),
fs = require('fs'),
app = express(),
port = 8080;
function getInt (num) {
return parseInt(num, 10);
}
function readJSONFile (path, callback) {
fs.readFile(__dirname + '/' + path, function(err, data) {
if (err) callback(err);
callback(null, JSON.parse(data));
});
}
app.get('/mem/services/rest/modules', function(req, res) {
readJSONFile('modules', function (err, json) {
if (err) res.send(404, err);
res.json(json);
});
});
app.get('/mem/services/rest/modules/:id', function(req, res){
var index;
function hasId(module, idx) {
if (module.id === getInt(req.params.id)) {
index = idx;
return true;
}
}
readJSONFile('modules', function (err, json) {
if (err) res.send(404, err);
json.some(hasId);
if (index) res.json(json[index]);
res.send(404);
});
});
app.listen(port);
console.log('Express server listening to', port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment