Include or Require in Nodejs to separate codes

Asked

Viewed 1,099 times

1

Hello! I wonder if in nodejs it is possible to separate some codes in different files as this example below, and how to do this:

Currently I have only one file this way:

File 111.js

var app     = require('../app');
var debug   = require('debug')('cancela:server');
var http    = require('http');
var b       = require('../config/gpio');
const bbbio = require('../config/bbb-io');

// Códigos iniciais
//...
//...

        var server = http.createServer(app);

        // Códigos para serem separados em outro arquivo

        var io = require('socket.io').listen(server);
        io.on('connection', function (socket) {
            socket.on('changeState', handleChangeState);
        });

        function handleChangeState(data) {
            var newData = JSON.parse(data);
            b.digitalWrite(bbbio.controleCancela, newData.state);
        }

// Outros códigos
//...
//...

Creating an additional "functions.js file"

functions.js

var io = require('socket.io').listen(server);
io.on('connection', function (socket) {
    socket.on('changeState', handleChangeState);
});

function handleChangeState(data) {
    var newData = JSON.parse(data);
    b.digitalWrite(bbbio.controleCancela, newData.state);
}

And including "functions.js" in "111.js" to look like below, but I’m not finding the right way to do "include" or "require":

111.js updated:

Arquivo 111.js 
var app     = require('../app');
var debug   = require('debug')('cancela:server');
var http    = require('http');
var b       = require('../config/gpio');
const bbbio = require('../config/bbb-io');

// Códigos iniciais
//...
//...
        var server = http.createServer(app);

        require('./functions');  <<<=== APENAS INCLUIR O CONTEÚDO DO ARQUIVO functions.js, SUBSTITUINDO O CÓDIGO ANTERIOR, MAIS NADA

// Outros códigos
//...
//...

2 answers

2

The part of requires is correct, but within each module you need to export the parts that will be public. For example:

arquivo1.js

function funcao() {

}
exports.funcao = funcao;

arquivo2.js

var funcoes = require('./arquivo1.js');
funcoes.funcao();

This article gives more details: http://nodebr.com/como-funciona-a-funcao-require-do-node-js/

0

With ES6 the syntax is different and can provide some other features, such as the import being asynchronous and being able to import only parts that are needed.

Syntax

Foobar.js

export function foo() {
  return 'bar';
}
export function bar() {
  return 'foo';
}

main.js

import {foo, bar} from 'foobar';
console.log(foo());
console.log(bar());
  • Lucas and @Notme, thank you. I ended up not commenting ,but I had already checked the documentation in addition to other staff posts and found these features. What I would really like is for the code to be replaced, literally, from the contents of the separate file foobar.js into the main.js, not to have to call the exported functions as in console.log(bar());.It would be the equivalent of what a "DEFINE" does in "C language". It is possible this in Nodejs or I am traveling?

  • 1

    @wBB does not give, the Node method is require + module.exports. The module system was designed so as not to leak anything pro global scope.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.