Wednesday, 19 April 2017

export mongodb is failed

I tried to export Mongodb instance db in the following format.But I am unable to get it .

Folder Structure

catering
  -server
    -server.js
    -mongoclient.js
  -package.json

These are my individual Files

mongoclient.js

var MongoClient = require('mongodb').MongoClient;  
var _db;
var exec = require("child_process").exec; 
var async = require('async');
var  series = require('async/series');
var file = __dirname + '/server.js';


async.series([
   function(callback){
       MongoClient.connect( "mongodb://localhost:27017/catering",                 function( err, db ) {
        _db = db;
        db.createCollection('posts');
        module.exports = {db:db};
        // callback(db + "11111");
        callback(null,123)
    });
  },
  function(callback){
    exec('nodemon '+ file,{},function(err,res){
        if(err){
            callback(null,err)
        }else{
            callback(null,res);
        }
    })
  }
],function(err,res){
  console.log("===========res",res,err)
})

Server.js

var express = require("express");
var app = express();
var fs = require('fs');
var path = require("path");
var db = require('./mongoclient.js');

app.listen(4000);

app.get('/',function(req,res){
  console.log("00000000000000000000get",db);    
    res.sendFile(path.join(__dirname + '/../client/index.html'))
});

Package.json

{
  "name": "catering",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
     "start": "npm-run-all --parallel connect:mongo  build:watch",
     "build:watch": "webpack --watch",
     "connect:mongo": "nodemon ./server/mongoclient.js",
     "test": "echo \"Error: no test specified\" && exit 1"
  },
 }



via Sai Ram

No comments:

Post a Comment