Created
January 26, 2014 02:18
-
-
Save hontas/8627227 to your computer and use it in GitHub Desktop.
Express server for fakeEnd
This file contains 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 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