Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* GLOBALS
- ----------------------------------------------------------------------*/
- var rdata = [];
- /* SERVER SETTINGS
- ----------------------------------------------------------------------*/
- //load http module to ceate an http server.
- var http = require('http');
- var url = require('url');
- //configure to respond http server with message
- http.createServer(function (request, response) {
- //request name parameter
- var url_parts = url.parse(request.url, true);
- var query = url_parts.query;
- //do the mongo
- var mongo = require('mongodb');
- var db = new mongo.Db('nodedb', new mongo.Server('localhost', 27017, {}), {});
- db.open(function() {
- db.collection('Persons', function(err, collection) {
- var cursor = collection.find(query);
- cursor.each(function(err, doc) {
- if(doc) {
- rdata.push(doc);
- }
- });
- });
- });
- //write what type of response
- response.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});
- //close response johan kvint :)
- response.end(JSON.stringify(rdata));
- //clear rdata
- rdata = [];
- }).listen(process.env.PORT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement