UNPKG

barterer

Version:

Real-time location & sensor data API for the IoT. We believe in an open Internet of Things.

80 lines (64 loc) 1.94 kB
/** * Copyright reelyActive 2015-2026 * We believe in an open Internet of Things */ const express = require('express'); const path = require('path'); const responseHandler = require('./responsehandler'); let router = express.Router(); router.route('/') .get((req, res) => { retrieveDevices(req, res); }); router.route('/:id/:type') .get((req, res) => { retrieveDevices(req, res); }); router.route('/:id/:type/:property') .get((req, res) => { retrieveDevices(req, res); }); /** * Retrieve the given devices. * @param {Object} req The HTTP request. * @param {Object} res The HTTP result. */ function retrieveDevices(req, res) { if(redirect(req.params.id, '', '', res)) { return; } switch(req.accepts(['json', 'html'])) { case 'html': res.sendFile(path.resolve(__dirname + '/../../web/barterer/devices/index.html')); break; default: let id = req.params.id; let type = req.params.type; let property = req.params.property; let rootUrl = req.protocol + '://' + req.get('host'); let queryPath = req.originalUrl; let devices = req.barterer.devices; devices.retrieve(id, type, property, (status, data) => { let response = responseHandler.prepareResponse(req, status, data); res.status(status).json(response); }); break; } } /** * Redirect if required and return the status. * @param {String} id The given ID. * @param {String} prefix The prefix to the ID in the path. * @param {String} suffix The suffix to the ID in the path. * @param {Object} res The HTTP result. * @return {boolean} Redirection performed? */ function redirect(id, prefix, suffix, res) { let standardisedId = null; // TODO: convert to standardised ID if(standardisedId && (standardisedId !== id)) { res.redirect(prefix + standardisedId + suffix); return true; } return false; } module.exports = router;