Skip to content

Instantly share code, notes, and snippets.

@nazrdogan
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazrdogan/69015ed9134872f742ab to your computer and use it in GitHub Desktop.
Save nazrdogan/69015ed9134872f742ab to your computer and use it in GitHub Desktop.
MongoDB read and stringfiy json
var http = require('http');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({ name: String, lastname: String, yas: Number, yer: String });
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
mongoose.connect('mongodb://localhost/bookmarks');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
var mUsers = mongoose.model('users', userSchema);
mUsers.find().lean().exec(function(err,users) {
sonuc=JSON.stringify(users);
console.log(sonuc);
if(req.url==='/index') {
console.log('Request Url'+req.url);
res.end(sonuc);
}
if(req.url==='/test'){
console.log('TEST');
}
db.close();
});
});
}).listen(1337,'127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment