Skip to content

Instantly share code, notes, and snippets.

@hontas

hontas/server.js

Created Jan 26, 2014
Embed
What would you like to do?
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
You can’t perform that action at this time.