Last active
December 20, 2015 02:19
-
-
Save iamtrk/6055731 to your computer and use it in GitHub Desktop.
Simple wiring of mongoose, Node.js and express
This file contains hidden or 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') | |
, http = require('http') | |
, foodlist = require('./foodlist'); | |
var app = express(); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.use(express.bodyParser()); | |
}); | |
app.get('/mnf/:manufacturer',function(req,res){ | |
console.log(req.params.manufacturer) | |
foodlist.fudlist(req.params.manufacturer, function(err,teams){ | |
console.log(teams.length+"This stuff is kool"+err); | |
res.send(teams[1].get("nutrients")); | |
}); | |
}); | |
http.createServer(app).listen(app.get('port'),function(){ | |
console.log("Server has started and is listening on "+app.get('port')); | |
}); |
This file contains hidden or 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 mongoose = require('mongoose'); | |
var db = mongoose.connection; | |
var messageSchema = mongoose.Schema({ | |
manufacturer:String | |
}); | |
exports.fudlist = function(manufacture, callback){ | |
mongoose.connect('mongodb://localhost/enron'); | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function() { | |
var essage = mongoose.model('foods',messageSchema); | |
essage.find({'manufacturer':manufacture}, function (err, teams) { | |
if(err){ | |
onErr(err,callback); | |
}else{ | |
mongoose.connection.close(); | |
console.log(teams.length); | |
callback("",teams) | |
} | |
}) | |
}); }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment