6
I know that it is possible to separate functions into other files on Node, called modules, as follows:
js server.
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-corpo.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 for code organization on the Node.
A practical example of what I imagine would be to access the variable n
body module, as if it had been declared in the file servidor.js
.
The intention is to organize the code, making, for example, a module only with variables, another only with routes, etc., so that it is easier to be treated/changed/expanded in the future.