V
V
Vladimir T.2013-12-27 02:19:51
Node.js
Vladimir T., 2013-12-27 02:19:51

Are there any "features" in node.js require?

Recently switched to node c php. I am writing a small REST server using ExpressJS. For simplicity, I decided to write only a few routers (so as not to prescribe for each method). Here is an example:

app.get('/v:version/:resource.:method',function(request,response){
    // подключаем метод
    var method = require('./'+request.params.resource+'.'+request.params.method+'v'+request.params.version+'.js')
    method.call(request,function(error,result){
      // отдаем данные или ошибку
    })
})

In this case, a call like myserver.com/v1/users.get should call the users.getv1.js module.
So I can easily add new methods and I just have to add a couple of lines in the documentation without adding a new router to the server code.
But someone from my acquaintances mentioned in a conversation that you should not do this because of some peculiarities of require modules will accumulate in the cache and memory will start to leak. He could not explain exactly what features, but I still had doubts.
The question is - are there really any "features" of require that the above approach will not allow to use? Or is it all nonsense? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2013-12-27
@AndyGrom

There will be no leaks of require itself. But the trouble is in your approach of the following character. require is synchronous. You are using a synchronous method in an asynchronous function - this is basically bad. All require calls must be placed at the beginning of the module so that they are called when the module is loaded. Read about good node.js practices, including on Habré. The conclusion is simple - synchronous code is evil.

V
Vladimir Kozhin, 2013-12-27
@affka

The memory shouldn't leak, @virpool is right. I would not recommend using requirejs for a node - it makes debugging a lot harder (when you really need to look for memory leaks).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question