Horváth, Győző
senior lecturer
horvath.gyozo@inf.elte.hu
Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government
Representational State Transfer (REST) Application Programming Interface (API)
GET
, POST
, DELETE
, PUT/PATCH
)GET /people
: querying all people
GET /people/john
: querying an element with john
identifier from model people
POST /people
: Inserting new element into people
modelDELETE /people/john
: Deleting element with john
identifier from model people
PUT /people/john
: Updating element with john
identifier in model people
Representational State Transfer
create → POST /collection
read → GET /collection[/id]
update → PUT /collection/id
patch → PATCH /collection/id
delete → DELETE /collection[/id]
Make a REST API endpoint for storing code snippets!
Use NeDB for storing the data!
Router
var express = require('express');
var router = express.Router();
var Datastore = require('nedb'),
db = new Datastore({ filename: 'snippets.nedb', autoload: true });
router
.get('/', function(req, res, next) {
})
.post('/', function(req, res) {
})
.put('/:id', function (req, res) {
})
.delete('/:id', function (req, res) {
});
module.exports = router;
var express = require('express');
var router = express.Router();
var Datastore = require('nedb'),
db = new Datastore({ filename: 'snippets.nedb', autoload: true });
router
.get('/', function(req, res, next) {
db.find({}, function (err, docs) {
res.json(docs);
});
})
.post('/', function(req, res) {
var doc = req.body;
db.insert(doc, function (err, newDoc) {
res.json(newDoc);
});
})
.put('/:id', function (req, res) {
var id = req.params.id;
var sn = req.body;
db.update({_id: id}, sn, {}, function (err, num, newDoc) {
res.json(newDoc);
});
})
.patch('/:id', function (req, res) {
var id = req.params.id;
var sn = req.body;
db.update({_id: id}, {$set: sn}, {}, function (err, num, newDoc) {
res.json(newDoc);
});
})
.delete('/:id', function (req, res) {
var id = req.params.id;
db.remove({_id: id}, {}, function (err, num) {
res.status(204).send('');
});
});
module.exports = router;
POST
methodGET
methodGET
methodPUT
and DELETE
methods