How to split code into multiple modules?

6

I know that it is possible to separate functions in other Node files, called modules, as follows:

server.js

var http = require("http");
var servidor = http.createServer();
var porta = 3000;

var corpo = require("./modulo-corpo.js");

servidor.on("request", function (request, response) {
    var resposta = corpo.getCorpo();
    response.writeHead(200, {
        "Content-Type": "text/plain",
        "Content-Length": resposta.length
    });
    response.end(resposta);
});

servidor.listen(porta, function () {
    console.log("servidor da bete, rodando na porta " + porta);
});

modulo-cuerpo.js

var n = 0;
exports.getCorpo = function() {
    return "bete beijou " + ++n + " bebados barrigudos bebendo bebidas baratas";
}
  

I would like to know what resources are available to organize the code in the Node.

A practical example of what I would imagine would be accessing the variable n of the body module, as if it had been declared in the file servidor.js .

  

The purpose is to organize the code, for example, a module with only variables, another with only routes, etc., so that it is easier to be treated / changed / expanded in the future.

    
asked by anonymous 07.05.2017 / 03:00

1 answer

4

As it is n is inaccessible to other modules and n behaves like private variable of the module. This is very useful in many cases.

If you want to see n you have to make getter that could be like this:

var n = 0;
exports.getCorpo = function() {
  return "bete beijou " + (++n) + " bêbados barrigudos bebendo bebidas baratas";
}
Object.defineProperty(exports, 'n', {
  get: function() {
    return n;
  }
});

So the module has a property n that will get the actual value of n without being overwritten.

If you want to manipulate n you can do a setter too:

Object.defineProperty(exports, 'n', {
  get: function() {
    return n;
  },
  set: function(valor) {
    n = valor;
  }
});

I've made an example online that you can download and test ( link ). The idea is:

file: server.js

const http = require('http');
const express = require('express');
const app = express();
const variaveis = require("./variaveis");
const rotas = require("./rotas")(app);

const server = app.listen(process.env.PORT || 3000, function(){
    const host = server.address().address;
    const port = server.address().port;
    console.log('App listening at http://%s:%s', host, port);
});

file: variaveis.js

let contador = 0;

const variaveis = Object.create(Object.prototype, {
  carregamento: { // data em que o servidor começou a correr
    writable: false,
    configurable: false,
    value: new Date() 
  },
  contador: {
    configurable: false,
    get: function() { return contador++ },
    set: function(valor) {
      contador = valor;
    }
  }
});

module.exports = variaveis;

file: rotas.js

const variaveis = require("./variaveis");
module.exports = (app) => {
    app.use('/', (req, res) => {
       res.end('O contador esta em: ' + variaveis.contador); 
    });

    app.use(function(req, res, next){
        console.log(req.originalUrl);
    });
}

With this structure it is possible for any file to know the value of contador as well as to change its value from outside the file where that variable is inserted.

    
07.05.2017 / 08:38