I previously had a single file named aaa.js
where the following code part existed, and separated in parts:
const callback = (err, data, res) => {
if (err) return console.log('ERRO: ', err);
res.writeHead(200, {'Content-Type': 'application/json'});
return res.end(JSON.stringify(data));
};
const getQuery = (reqUrl) => {
const url_parts = url.parse(reqUrl);
return querystring.parse(url_parts.query);
};
const find = (req, res) => {
const query = getQuery(req.url);
MeuObjeto.find(query, (err, data) => callback(err, data, res));
};
const CRUD = {
find
};
module.exports = CRUD;
After sorting codes to organize better, I created four different files as follows:
aaa.js
const find = require('./../actions/action-find')(MeuModel);
const CRUD = {
find
};
module.exports = CRUD;
find.js
module.exports = (MeuModel) => {
return (req, res) => {
const query = getQuery(req.url); // <========= Como fica essa chamada?
MeuModel.find(query, (err, data) => callback(err, data, res)); // <========= Como fica essa chamada?
};
};
callback.js
module.exports = (err, data, res) => {
if (err) return console.log('ERRO: ', err);
res.writeHead(200, {'Content-Type': 'application/json'});
return res.end(JSON.stringify(data));
};
get-query.js
module.exports = (reqUrl) => {
const url_parts = url.parse(reqUrl);
return querystring.parse(url_parts.query);
};
DOUBT : How do I call the "callback" and "get-query" functions from within the "find.js" file as pointed out in the code comment?