0

For simplicity, I've created 3 files. I want to include them accordingly. But I don't know how to do it. For example, I want server.js to include routes.js, and routes.js would need handlers.js So that I can execute server.js successfully. I tried to include them using require but it showed errors like server is not defined. Is there any way to do it?

Here are my files:

server.js

var restify = require('restify');
var server = restify.createServer();
require('./routes.js');
server.listen(8081, function () {
    console.log('%s is listening at %s', server.name, server.url);
});

routes.js

server.get('/get', getHandler);
server.put('/update', putHandler);
server.post('/post', postHandler);
server.delete('/delete', delHandler);

handlers.js

function getHandler(req, res, next){
    connectionPool.getConnection(function(err, connection){
        if(err){
            res.send({
                Error: err,
                Message: "Can't connect Database."
            });
        }else{
            //queries
            connection.query("SELECT * FROM book", function(err, rows, fields){
                res.send(rows);
            })
        }
    })
}

NOTE: Some users have marked it duplicate but that answer doesn't solve my problem. That answer doesn't explain how to include A into B and B into A. I need routes.js to include server.js and server.js to include routes.js. How can I do this? The anser only explain how to include a function.

user3.14
  • 371
  • 2
  • 15
  • It's not duplicate. That answer just show how to include a function. That doesn't explain how to include A into B and B into A. – user3.14 Nov 04 '16 at 10:24
  • Some users have marked it duplicate but that answer doesn't solve my problem. That answer doesn't explain how to include A into B and B into A. I need routes.js to include server.js and server.js to include routes.js. How can I do this? The anser only explain how to include a function. – user3.14 Nov 04 '16 at 10:27
  • At first you should require server.js and handlers.js modules in routes.js, node.js modules arent working like php code, you have to require every module in every place you use it – styopdev Nov 04 '16 at 10:27
  • @VikasKumar you have to use `require()`, there is no "include" in Node. – robertklep Nov 04 '16 at 10:30
  • 1
    Do not close this, his question is valid and the other answer does not solve it's problem. He needs it's inner module to have access to the parent variables. It's need to explained how to do this. – O_Z Nov 04 '16 at 10:30
  • @styopdev require doesn't work. – user3.14 Nov 04 '16 at 10:31
  • 1
    @VikasKumar send your server as a parameter to an init function inside routes.js. Would write your some code, but they closed it – O_Z Nov 04 '16 at 10:31
  • @VikasKumar there is no require in routes.js you have provided – styopdev Nov 04 '16 at 10:33
  • Could you please explain a little more? What is init function? – user3.14 Nov 04 '16 at 10:34
  • @O_Z the question for which this one is marked as a duplicate does show how to export a function. It should follow that you would be able to pass variables, like `server`, to such a function. – robertklep Nov 04 '16 at 10:34
  • If there's no require in routes.js that doesn't mean I didn't try it. @styopdev – user3.14 Nov 04 '16 at 10:34
  • @VikasKumar if you don't know how modules, or functions, work in Node, you should probably start with a tutorial on the matter. – robertklep Nov 04 '16 at 10:38
  • @O_Z it worked for those 2 files. I read your edit. Now I'm getting error 'getHandler' is not defined. It looks more complicated now. How can I do it? – user3.14 Nov 04 '16 at 11:21
  • @robertklep now I'm getting error 'getHandler' is not defined. Do you think that question can solve it? – user3.14 Nov 04 '16 at 11:27
  • 1
    @VikasKumar yes; the `getHandler` function needs to be exported (and subsequently `require`'d in `routes.js`), but the principle is very much the same. – robertklep Nov 04 '16 at 11:28
  • 1
    No I only solved your server Error. Check solution again I added aline about it.This is really standart. – O_Z Nov 04 '16 at 11:29
  • @robertklep @O_Z I moved my all 4 handlers (not listed in this question's details) inside a new function `some` with 4 parameters named same as handler functions. Then I wrote `exports.some=some` and finally required in routes.js by writing: `require('./handlers.js').some(4 parameters)` but it doesn't work. It still shows handler not defined. – user3.14 Nov 04 '16 at 11:35
  • @VikasKumar [here's an example](https://gist.github.com/robertklep/7572ccf3990f580333005c6db168c145) – robertklep Nov 04 '16 at 11:41
  • Now it is showing that getHandler is an unexpected identifier (in handlers.js) – user3.14 Nov 04 '16 at 11:53
  • I've tried these things but still I'm facing problem. I've shared it here: http://stackoverflow.com/questions/40422845/why-is-there-connectionpool-not-defined-error Could you please check? – user3.14 Nov 04 '16 at 12:36
  • Please somebody check the above link. – user3.14 Nov 04 '16 at 13:24

0 Answers0