Advertisement
harman123

harman

Mar 30th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /*
  2. * Module dependencies
  3. */
  4. var express = require('express')
  5. var stylus = require('stylus')
  6. var nib = require('nib')
  7. var app = express()
  8. var MongoClient = require('mongodb').MongoClient
  9. var assert = require('assert');
  10. var mongoDbObj;
  11. var mongoose = require('mongoose');
  12. var Schema = mongoose.Schema;
  13. mongoose.connect('mongodb://localhost:3000');
  14. var db = mongoose.connection;
  15. db.on('error', console.error.bind(console, 'connection error:'));
  16. db.once('open', function (callback) {
  17. // yay!
  18. });
  19. var blogSchema = mongoose.Schema({
  20. name: String
  21.  
  22. })
  23. var MongoClient = require('mongodb').MongoClient
  24. , assert = require('assert');
  25.  
  26. db.open(function(err, client){
  27. client.createCollection("docs", function(err, col) {
  28. client.collection("docs", function(err, col) {
  29. for (var i = 0; i < 100; i++) {
  30. col.insert({c:i}, function() {});
  31. }
  32. });
  33. });
  34. });
  35. // Fetch a collection to insert document into
  36. db.open(function(err, db) {
  37. var collection = db.collection("simple_document_insert_collection_no_safe");
  38. // Insert a single document
  39. collection.insert({hello:'world_no_safe'});
  40.  
  41. // Wait for a second before finishing up, to ensure we have written the item to disk
  42. setTimeout(function() {
  43.  
  44. // Fetch the document
  45. collection.findOne({hello:'world_no_safe'}, function(err, item) {
  46. assert.equal(null, err);
  47. assert.equal('world_no_safe', item.hello);
  48. db.close();
  49. })
  50. }, 100);
  51. });
  52.  
  53. // Connection URL
  54. var url = 'mongodb://localhost:27017/myproject';
  55. // Use connect method to connect to the Server
  56. MongoClient.connect(url, function(err, db) {
  57. if(err)
  58. console.log(err);
  59. else{
  60. console.log("Connected to MongoDB");
  61. assert.equal(null, err);
  62. console.log("Connected correctly to server");
  63. db.close();
  64.  
  65. }
  66. mongoDbObj={db: db,
  67. blog: db.collection('blog')
  68. };
  69.  
  70. });
  71. var blogSchema = new Schema({
  72. title: String,
  73. author: String,
  74. body: String,
  75. comments: [{ body: String, date: Date }],
  76. date: { type: Date, default: Date.now },
  77. hidden: Boolean,
  78. meta: {
  79. votes: Number,
  80. favs: Number
  81. }
  82. });
  83.  
  84. var blog = mongoose.model('blog', blogSchema)
  85. function compile(str, path) {
  86. return stylus(str)
  87. .set('filename', path)
  88. .use(nib())
  89. }
  90. app.set('views', __dirname + '/views')
  91. app.set('view engine', 'jade')
  92. app.use(express.logger('dev'))
  93. app.use(stylus.middleware(
  94. { src: __dirname + '/public'
  95. , compile: compile
  96. }
  97. ))
  98. app.use(express.static(__dirname + '/public'))
  99. app.get('/', function (req, res) {
  100. res.end('Blog on Node.js')
  101. })
  102. app.listen(3000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement