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.