Skip to content

Instantly share code, notes, and snippets.

@nazrdogan
Created May 11, 2014 21:27
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/0420e0640875932df33c to your computer and use it in GitHub Desktop.
Save nazrdogan/0420e0640875932df33c to your computer and use it in GitHub Desktop.
MongoDB express.js
var express, cons, mongoclient, app,Server;
express = require('express');
cons = require('consolidate');
MongoClient = require('mongodb').MongoClient;
Server=require('mongodb').Server;
app = express();
app.engine('html',cons.swig);
app.set('view engine','html');
app.set('views',__dirname+'/views');
var mongoclient=new MongoClient(new Server('localhost',27017,{native_parse:true}));
var db=mongoclient.db('app');
app.get('/test.json',function(req,res){
res.header("Content-Type", "application/json; charset=utf-8");
db.collection('haber').find({},{ _id: 0 }).toArray(function(err,doc){
var sonuc=JSON.stringify(doc);
res.send(sonuc);
});
});
app.get('*',function(req,res){
res.send('page not found',404);
});
mongoclient.open(function(err,mongoclient) {
if(err) throw err
app.listen(8080);
console.log('Server is Runnig,Bitch');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment