1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Drasko DRASKOVIC 92b4a70528 Correct headers for token. Send full device JSON on creation, update and delete
Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
2015-10-11 22:35:05 +02:00

96 lines
2.5 KiB
JavaScript

var express = require('express');
var router = express.Router(); // get an instance of the express Router
var Device = require('../models/device');
/**
* /devices
*/
router.route('/')
/** Create a device (accessed at POST http://localhost:8080/devices) */
.post(function(req, res) {
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
var device = new Device(); // create a new instance of the Device model
device.name = req.body.name; // set the device's name (comes from the request)
/** Save the device and check for errors */
device.save(function(err) {
if (err)
res.send(err);
res.json(device);
});
})
/** Get all the devices (accessed at GET http://localhost:8080/devices) */
.get(function(req, res) {
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
Device.find(function(err, devices) {
if (err)
res.send(err);
res.json(devices);
});
});
/**
* /devices/:device_id
* N.B. Colon (`:`) is needed because of Express `req.params`: http://expressjs.com/api.html#req.params
*/
router.route('/:device_id')
/** Get the device with that id (accessed at GET http://localhost:8080/devices/:device_id) */
.get(function(req, res) {
Device.findById(req.params.device_id, function(err, device) {
if (err)
res.send(err);
res.json(device);
});
})
/** Update the device with this id (accessed at PUT http://localhost:8080/devices/:device_id) */
.put(function(req, res) {
/** Use our device model to find the device we want */
Device.findById(req.params.device_id, function(err, device) {
if (err)
res.send(err);
device.name = req.body.name; // update the devices info
/** Save the device */
device.save(function(err) {
if (err)
res.send(err);
res.json(device);
});
})
})
/** Delete the device with this id (accessed at DELETE http://localhost:8080/devices/:device_id) */
.delete(function(req, res) {
Device.remove({
_id: req.params.device_id
}, function(err, device) {
if (err)
res.send(err);
res.json(device);
});
});
/**
* Export router module
*/
module.exports = router;