mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00
Added config and routes dirs
This commit is contained in:
parent
17adff355c
commit
c8c61bfe4a
15
app/routes/status.js
Normal file
15
app/routes/status.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router(); // get an instance of the express Router
|
||||||
|
|
||||||
|
// on routes that end in /things
|
||||||
|
// ----------------------------------------------------
|
||||||
|
router.route('/')
|
||||||
|
|
||||||
|
// get the status (accessed at GET http://localhost:8080/status)
|
||||||
|
.get(function(req, res) {
|
||||||
|
var stat = {"status":"OK"}
|
||||||
|
res.send(stat);
|
||||||
|
});
|
||||||
|
|
||||||
|
// export router module
|
||||||
|
module.exports = router;
|
85
app/routes/things.js
Normal file
85
app/routes/things.js
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router(); // get an instance of the express Router
|
||||||
|
|
||||||
|
var Thing = require('../models/thing');
|
||||||
|
|
||||||
|
// on routes that end in /things
|
||||||
|
// ----------------------------------------------------
|
||||||
|
router.route('/')
|
||||||
|
|
||||||
|
// create a things (accessed at POST http://localhost:8080/things)
|
||||||
|
.post(function(req, res) {
|
||||||
|
|
||||||
|
var thing = new Thing(); // create a new instance of the Bear model
|
||||||
|
thing.name = req.body.name; // set the thing's name (comes from the request)
|
||||||
|
|
||||||
|
// save the thing and check for errors
|
||||||
|
thing.save(function(err) {
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
|
||||||
|
res.json({ message: 'Thing created!' });
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
// get all the things (accessed at GET http://localhost:8080/things)
|
||||||
|
.get(function(req, res) {
|
||||||
|
Thing.find(function(err, things) {
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
|
||||||
|
res.json(things);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// on routes that end in /things/:thing_id
|
||||||
|
// ----------------------------------------------------
|
||||||
|
router.route('/:thing_id')
|
||||||
|
|
||||||
|
// get the thing with that id (accessed at GET http://localhost:8080/things/:thing_id)
|
||||||
|
.get(function(req, res) {
|
||||||
|
Thing.findById(req.params.thing_id, function(err, thing) {
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
res.json(thing);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
// update the thing with this id (accessed at PUT http://localhost:8080/things/:thing_id)
|
||||||
|
.put(function(req, res) {
|
||||||
|
|
||||||
|
// use our thing model to find the thing we want
|
||||||
|
Thing.findById(req.params.thing_id, function(err, thing) {
|
||||||
|
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
|
||||||
|
thing.name = req.body.name; // update the things info
|
||||||
|
|
||||||
|
// save the thing
|
||||||
|
thing.save(function(err) {
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
|
||||||
|
res.json({ message: 'Thing updated!' });
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// delete the thing with this id (accessed at DELETE http://localhost:8080/things/:thing_id)
|
||||||
|
.delete(function(req, res) {
|
||||||
|
Thing.remove({
|
||||||
|
_id: req.params.thing_id
|
||||||
|
}, function(err, thing) {
|
||||||
|
if (err)
|
||||||
|
res.send(err);
|
||||||
|
|
||||||
|
res.json({ message: 'Successfully deleted' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// export router module
|
||||||
|
module.exports = router;
|
4
config/config.js
Normal file
4
config/config.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/*
|
||||||
|
* Following recipe here: http://dailyjs.com/2014/01/02/recipe-for-express-configuration/
|
||||||
|
*/
|
||||||
|
module.exports = require('./' + (process.env.NODE_ENV || 'development') + '.json');
|
9
config/development.json
Normal file
9
config/development.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"message" : "We are in development",
|
||||||
|
"db" : {
|
||||||
|
"path" : "mongodb://localhost",
|
||||||
|
"port" : "27017",
|
||||||
|
"name" : "test"
|
||||||
|
},
|
||||||
|
"port" : "8080"
|
||||||
|
}
|
0
config/production.json
Normal file
0
config/production.json
Normal file
0
config/test.json
Normal file
0
config/test.json
Normal file
114
server.js
114
server.js
@ -1,5 +1,12 @@
|
|||||||
// server.js
|
// server.js
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extrenal configs are kept in the config.js file on the same level
|
||||||
|
*/
|
||||||
|
var config = require('./config/config');
|
||||||
|
console.log(config.message);
|
||||||
|
|
||||||
|
|
||||||
// BASE SETUP
|
// BASE SETUP
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
@ -10,120 +17,21 @@ var bodyParser = require('body-parser');
|
|||||||
|
|
||||||
// MongoDB
|
// MongoDB
|
||||||
var mongoose = require('mongoose');
|
var mongoose = require('mongoose');
|
||||||
mongoose.connect('mongodb://localhost:27017/mainflux'); // connect to our database
|
|
||||||
|
|
||||||
|
mongoose.connect(config.db.path + ':' + config.db.port + '/' + config.db.name); // connect to our database
|
||||||
|
|
||||||
var Thing = require('./app/models/thing');
|
|
||||||
|
|
||||||
// configure app to use bodyParser()
|
// configure app to use bodyParser()
|
||||||
// this will let us get the data from a POST
|
// this will let us get the data from a POST
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
var port = process.env.PORT || 8080; // set our port
|
var port = process.env.PORT || config.port; // set our port
|
||||||
|
|
||||||
// ROUTES FOR OUR API
|
// ROUTES FOR OUR API
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
var router = express.Router(); // get an instance of the express Router
|
app.use('/status', require('./app/routes/status'));
|
||||||
|
app.use('/things', require('./app/routes/things'));
|
||||||
// on routes that end in /things
|
|
||||||
// ----------------------------------------------------
|
|
||||||
router.route('/things')
|
|
||||||
|
|
||||||
// create a things (accessed at POST http://localhost:8080/api/things)
|
|
||||||
.post(function(req, res) {
|
|
||||||
|
|
||||||
var thing = new Thing(); // create a new instance of the Bear model
|
|
||||||
thing.name = req.body.name; // set the thing's name (comes from the request)
|
|
||||||
|
|
||||||
// save the thing and check for errors
|
|
||||||
thing.save(function(err) {
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
|
|
||||||
res.json({ message: 'Thing created!' });
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
// get all the things (accessed at GET http://localhost:8080/api/things)
|
|
||||||
.get(function(req, res) {
|
|
||||||
Thing.find(function(err, things) {
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
|
|
||||||
res.json(things);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// on routes that end in /things/:thing_id
|
|
||||||
// ----------------------------------------------------
|
|
||||||
router.route('/things/:thing_id')
|
|
||||||
|
|
||||||
// get the thing with that id (accessed at GET http://localhost:8080/api/things/:thing_id)
|
|
||||||
.get(function(req, res) {
|
|
||||||
Thing.findById(req.params.thing_id, function(err, thing) {
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
res.json(thing);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
// update the thing with this id (accessed at PUT http://localhost:8080/api/things/:thing_id)
|
|
||||||
.put(function(req, res) {
|
|
||||||
|
|
||||||
// use our thing model to find the thing we want
|
|
||||||
Thing.findById(req.params.thing_id, function(err, thing) {
|
|
||||||
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
|
|
||||||
thing.name = req.body.name; // update the things info
|
|
||||||
|
|
||||||
// save the thing
|
|
||||||
thing.save(function(err) {
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
|
|
||||||
res.json({ message: 'Thing updated!' });
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// delete the thing with this id (accessed at DELETE http://localhost:8080/api/things/:thing_id)
|
|
||||||
.delete(function(req, res) {
|
|
||||||
Thing.remove({
|
|
||||||
_id: req.params.thing_id
|
|
||||||
}, function(err, thing) {
|
|
||||||
if (err)
|
|
||||||
res.send(err);
|
|
||||||
|
|
||||||
res.json({ message: 'Successfully deleted' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// middleware to use for all requests
|
|
||||||
router.use(function(req, res, next) {
|
|
||||||
// do logging
|
|
||||||
console.log('Something is happening.');
|
|
||||||
next(); // make sure we go to the next routes and don't stop here
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
|
|
||||||
router.get('/', function(req, res) {
|
|
||||||
res.json({ message: 'hooray! welcome to our api!' });
|
|
||||||
});
|
|
||||||
|
|
||||||
// more routes for our API will happen here
|
|
||||||
|
|
||||||
// REGISTER OUR ROUTES -------------------------------
|
|
||||||
// all of our routes will be prefixed with /api
|
|
||||||
app.use('/api', router);
|
|
||||||
|
|
||||||
|
|
||||||
// START THE SERVER
|
// START THE SERVER
|
||||||
|
Loading…
x
Reference in New Issue
Block a user