add get devices routes/controller

This commit is contained in:
Francesco Esposito 2019-02-19 19:21:19 +01:00
parent 64aa61a027
commit f3842099e1
3 changed files with 29 additions and 3 deletions

10
apiRoutes.js Normal file
View File

@ -0,0 +1,10 @@
const express = require('express');
const deviceController = require('./controllers/deviceController');
const router = express.Router();
router
.route('/devices')
.get(deviceController.index);
module.exports = router;

View File

@ -0,0 +1,13 @@
const Device = require('../models/deviceModel');
function index(req, res) {
Device.get((err, devices) => {
if (err) {
res.status(500).send();
}
res.json(devices);
});
}
module.exports.index = index;

View File

@ -2,6 +2,7 @@ const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const config = require('./config');
const apiRoutes = require('./apiRoutes');
const { db } = config;
const app = express();
@ -16,9 +17,11 @@ mongoose.set('useFindAndModify', false);
const mongodb = mongoose.connection;
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', apiRoutes);
app.listen(port, () => {
console.log(`trackmyd-api running. PORT: ${port}`);
});
console.log(`trackmyd-api running. PORT: ${port}`);
});