Currently I'm running a node js backend with express to access the mysql database from the client. Now I'm refactoring my code to put the "fat" things from the client also to the nodejs backend. My first idea is to keep the express route like:
ModuleA:
router.get('/getitem/:code',(req, res) => {
let sql = `SELECT * FROM xyz WHERE CODE = ${req.params.code}`;
let query = db.query(sql, (err, result) =>{
if(err == null){
console.log(result);
res.send(result);
}else{
console.log("Error by getting item from db: " + err);
throw err;
}
});
});
And access it via http request by a other module if needed:
function geodataByLocationcode(locationcode){
request({
method: 'GET',
url: "http://" + server_connection.host + ":" + server_connection.port + '/getitem/' + locationcode,
headers: {
"Content-Type": "application/json"
}
},function(error, response, body){
....
}
But is this the best method to do this? Or would it be better to access the db in a more direct way since the request now also comes from the backend?