Last active
June 22, 2017 09:53
-
-
Save jvadillo/1202cd29880bcff3a4236e0721ecbca9 to your computer and use it in GitHub Desktop.
Express + Node.js + Mongodb: get parametter attribute list and search by $in operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
/** | |
* Get events from the REST webservice | |
* [GET] url: https://mydomain.com/event/params?eventType=festival,gallery,show | |
*/ | |
router.get('/event', function(req, res) { | |
var query = {}; //query for MongoDB | |
var arr,subq; | |
var eventType; | |
if (req.query.eventType) { | |
eventType = req.query.eventType; //get value of eventType param from the request object. | |
//Transform tCSV received to Array | |
subq = {}; | |
subq["$in"] = []; | |
arr = eventType.split(','); | |
//Create subquery | |
subq["$in"] = arr; | |
//Add the subquery to the query object | |
query["eventType"]= subq; | |
} | |
//Get the DB and then the collection: | |
var db = req.db; | |
var collection = db.collection('events'); | |
//Find and return the response. | |
collection.find(query).toArray(function(err, docs) { | |
if( err || !docs) { | |
console.log("No events found"); | |
res.end("No events found for: "+query); | |
} | |
else { | |
console.log(docs); | |
res.json({ events: docs }); | |
} | |
}); | |
}); | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A simple Nodejs and Express server using MongoDB. | |
*/ | |
var http = require('http'); | |
var path = require('path'); | |
var cors = require('cors'); | |
var bodyParser = require('body-parser'); | |
var express = require('express'); | |
//DB init (MongoDB) | |
var db = {}; | |
var MongoClient = require('mongodb').MongoClient | |
, assert = require('assert'); | |
// Connection URL | |
var url = 'mongodb://myhost:27017/mydatabase'; | |
// Use connect method to connect to the server | |
MongoClient.connect(url, function(err, thisdb) { | |
db = thisdb; | |
assert.equal(null, err); | |
console.log("Connected successfully to server"); | |
}); | |
var app = express(); //Creates an Express application | |
var server = http.createServer(app); //calls http.Server() internally and returns the resulting instance. | |
//Express configuration: | |
app.use(express.static(path.resolve(__dirname, 'client'))); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.use(cors()); | |
// Make our db accessible to our router | |
app.use(function(req,res,next){ | |
req.db = db; | |
next(); | |
}); | |
//Routes | |
var events = require('./routes/events'); | |
app.use('/', events) | |
//Bind and listen for connections on the given host and port | |
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){ | |
var addr = server.address(); | |
console.log("Swingnow server listening at", addr.address + ":" + addr.port); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment