Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active March 31, 2017 04:15
Show Gist options
  • Save chrisdel101/3f3307fbd4e20f7d474a70ab6b68b71c to your computer and use it in GitHub Desktop.
Save chrisdel101/3f3307fbd4e20f7d474a70ab6b68b71c to your computer and use it in GitHub Desktop.
Mongo Node
MONGO DATA (doglist)
{ "_id" : ObjectId("58dc7505cefb0da47eab5205"), "name" : "buddy", "breed" : "white lab", "description" : "loves to play ball" }
INDEX.JS
var express = require('express');
var router = express.Router();
var mongo = require('mongodb')
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongo.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://localhost:27017/mongoTestExpress';
// Connect to the server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the Server', err);
} else {
// We are connected
console.log('Connection established to', url);
// Get the documents collection
var collection = db.collection('dogs');
// console.log(collection)
// Find all dogs
collection.find({}).toArray(function (err, result) {
if (err) {
res.send(err);
} else if (result.length) {
res.render('doglist', ({"doglist":result}));
} else {
res.send('No documents found');
}
//Close connection
db.close();
})
}
})
})
DOGLIST.EJS
<h3>Dogs</h3>
<% doglist.forEach(function(dog) { %>
<%= dog.name %>
<% }) %>
LAYOUT.EJS
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet', href='/stylesheets/style.css' />
<meta charset="utf-8">
<title></title>
</head>
<body>
<%- body %>
</body>
</html>
APP.JS
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongo = require("mongodb");
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment