In php,c++, there is the concept of namespaces, namespaces are mainly used to solve the introduction of file existence function, class, variable name of the problem, in node. js, there is no namespace so complex concept, in node, there is the concept of module, that is, the functional code is put together. It is then introduced and used in this document. This does not occur in the function name, variable names problem, in the introduction of the module, are registered in the form of import. Look at the following example:
// Circle.js var pi = 3.14; // calculate the perimeter of a circle function (r) { return 2 * pi * r;} // calculate the area of a circle function (r) { return pi * R * r;} ~
var circle = require ('./circle '); Console.log (Circle.circle (2)); Console.log (Circle.area ( 3)); ~
1, so the above pi in the introduction of the file is not useful, it only in the Circle.js local effective. Then exports. Set the properties, and then you can use the callback function with these two functions
Attention:
1, if the directory is introduced, the default is to introduce the directory under the Index.js this file, or to specify the module name, you can omit the. js suffix.
2, using require to introduce the module, this is an IO synchronous operation, so it is best to introduce at the beginning of the file, if the IO volume is large, the introduction, there will be a series of problems such as blocking. Performance degradation.
node. js in module, require