-
-
Save azendal/4449979 to your computer and use it in GitHub Desktop.
Small CORS JSON Server
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'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
app.get('/:model', function (req, res) { | |
res.json(data[req.params.model]); | |
}); | |
app.get('/:model/:id', function (req, res){ | |
var record = data[req.params.model].filter(function (entry) { | |
return entry.id == req.params.id; | |
}); | |
res.json(record[0]); | |
}); | |
app.post('/:model', function (req, res){ | |
var record = JSON.parse(req.param('entries')); | |
record.id = '123456'; | |
data[req.params.model].push(record); | |
res.json(record); | |
}); | |
app.put('/:model/:id', function (req, res){ | |
var record = data[req.params.model].filter(function (entry) { | |
return entry.id == req.params.id; | |
}); | |
var position = data[req.params.model].indexOf(record); | |
data[req.params.model].splice(position, 1, JSON.parse(req.param[req.param.model])); | |
res.json(data[req.params.model][position]); | |
}); | |
app.delete('/:model/:id', function (req, res){ | |
var record = data[req.params.model].filter(function (entry) { | |
return entry.id == req.params.id; | |
}); | |
data[req.params.model].splice(data[req.params.model].indexOf(record), 1); | |
res.json({}); | |
}); | |
app.listen(3000); | |
console.log('listening'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment