var express = require('express'); var router = express.Router(); var debug = require('debug')('Icarus:server'); var mongoClient = require('mongodb').MongoClient; var assert = assert = require('assert'); // Connection URL var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus'; //Use connect method to test the connection to the Server mongoClient.connect(mongoUrl, function(err, db) { assert.equal(null, err, err); debug("Connected to MongoDB database: "+db.databaseName); db.close(); }); router.get('/:resource_id', function(req, res, next) { res.sendStatus(200); }); router.post('/', function(req, res, next){ mongoClient.connect(mongoUrl, function(err, db) { if(err!=null){ debug(err); next(err); }else{ var collection = db.collection('resources'); var resource = req.body; if(Object.keys(resource).length>0) { collection.insertOne(resource, function (err, r) { if (err != null) { debug(err); next(err); } else { db.close(); res.sendStatus(201); } }); }else{ var error = new Error("You can't store an empty resource!"); error.status = 400; debug(error); next(error); } } }); }); module.exports = router;