1

this is demo.js file and i want to use this file in server.js file so that i can use diffrent js files in one server file.

Demo.js:

    app.get('/add User', function (req, res) {
    var MongoClient = mongodb.MongoClient;
    var url = 'mongodb://localhost:27017/project';
    MongoClient.connect(url, function (err, db) {
             var collection = db.collection('users');
            collection.find({name: 'shruti'}).toArray(function (err, result) {
                console.log(, result);
                    db.close();
        });

Server.js:

    var a = require('./demo.js');
    vr http=require("http");

    var server = http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/html"});
      response.write(a);
      res.end();});
    server.listen(7860);
3
  • And what is the problem? Commented Jan 21, 2016 at 10:23
  • @shruti-dengre To make something exportable, use module.exports in your Demo.js , but you also need a promise for whatever you're trying to accomplish here, since there is no guarantee that a will be populated before the write function gets fired. Read Javascript Promises or change your workflow. Commented Jan 21, 2016 at 10:43
  • if possible, add complete demo.js and app.js Commented Jan 21, 2016 at 18:54

2 Answers 2

1

A possible sample would look like :

demo.js

var myModule = {
  defineRoutes: function(router){
    //do something...
  }
}

module.exports = myModule;

server.js

var myModule = require('demo.js');
myModule.defineRoutes(router);
Sign up to request clarification or add additional context in comments.

Comments

0

As stated, you need to export. When you do:

var item = require("mymodule");

Require returns an object, which is a reference the value of module.exports for that given file - in your case demo.js.

You can write your modules a few ways as some people have shown you. Because it is encapsulated you basically are identifying what is public or can be called. Few ways to write it - you could also do:

module.exports = {
  yourCall: function () {
     console.log("stuff here");
  }
};

As stated by @ishann, who is dead on here, you are writing something you assume might be populated. Going to a database and returning is an asynchronous call - so it will take time to go do that and then for the results to be returned.

Based on your structure - ideally what you want to do is assign the route ( "/addUser" ) which will pass in the response object to you:

app.get('/add User', function (req, res) {
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/project';
MongoClient.connect(url, function (err, db) {
         var collection = db.collection('users');
        collection.find({name: 'shruti'}).toArray(function (err, result) {
            console.log(, result);
                db.close(); 
                 // set the type
                 res.writeHead(200, {"Content-Type": "application/json"});
                 res.write(result);

    });

Just looks like your code needs a bit of reorg, but separting concerns is good. You might want to also check out Express as a framework for node.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.