0

I have a NodeJS application and I want to execute some method for file validations but just one time (this method will validate some files under the node application).

Is there an elegant way to do this? Events?

ssube
  • 47,010
  • 7
  • 103
  • 140
  • 1
    A module loaded via a given pathname is only loaded once, so you can do your file validation inside a module that's required. – Pointy Jul 13 '15 at 14:51
  • @Pointy can you please provide example since I Need to invoke function inside the module to do the validations... –  Jul 13 '15 at 14:53
  • @Pointy - why did you delete you answer :) –  Jul 13 '15 at 15:01
  • 1
    I deleted mine because @ssube gave a better one with the same information as me, except he/she is apparently an actual Node user while I'm not. – Pointy Jul 13 '15 at 15:05
  • 1
    @Pointy "Actual node user" might be a bit liberal. :P Your answer did remind me that I'd forgotten to export anything from the validator. Really, I don't think this question is all that node-specific, although the convenient browser events are missing in a node environment. – ssube Jul 13 '15 at 15:07

1 Answers1

2

The NodeJS documentation on modules states that:

Modules are cached after the first time they are loaded.

which you can take advantage of by adding static code to the module. Regardless of how many times the module is loaded, the static code will retain its state(/value).

You can use that state from a method to implement a method that can be called whenever you like -- at the best time during initialization -- but only ever be called once. This is pretty simple:

var called = false;

function checkFiles() {
  if (called) return;

  // Perform your validation
  called = true;
}

module.exports = {
  checkFiles: checkFiles
};

Because of the module caching, you can require this file in as many places as you need and it will still only execute once.

To invoke this function, you have a few options:

For a simple application, you can call the function from your main module (or function) and it will be invoked at that time. If the validation should be asynchronous, you can wrap your main method in a function and pass that to the validator as a callback.

//#! /bin/env node
var express = require('express');
var validator = require('./validator');

validator.checkFiles();
var app = express();

var server = app.listen(3000, function () {
  ...
});

For a more complicated application, you should call this function during your existing initialization routine (again, using callbacks as necessary).

If you have a nice modern promise-based initializer, you could simply add the validator as the first step of the chain.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • Thanks voted up,assume that I provided this module do you suggest to create new folder like utils and put it there or...?where is the place to put internal validation module in node js application (best practice ...) –  Jul 13 '15 at 15:02
  • 1
    You can organize modules however you like (or not at all). If you have many validators that do different things, a `utils/validator` directory with a few different classes might be the clearest structure. That's really up to you. – ssube Jul 13 '15 at 15:03
  • Thanks ssube ,you helped a lot,I've additional question ,can you help please ?http://stackoverflow.com/questions/31387815/call-to-modules-in-specific-order-in-node –  Jul 13 '15 at 15:48