I'm currently building my first API using node.js and the express framework. With this API I want to retrieve data from an SQL database. I think that the basis is right, things like the routing for the HTTP calls, the connection to the database and the first SQL test calls.
The only part I'm having trouble with is the connection between the ..(class)... controller and the ..(class).. itself. I've searched (and found) a lot of good examples and guides for the basics but struggle on this part. I will shortly explain the files and try to explain my problem ass clear as possible!
controller is responsible for: the HTTP routing
class is responsible for MySQL calls to the DB
note: there will be more controller & classes to make the code better manageable
The code I currently have is ass follows:
CategorieController.js
let express = require('express'),
router = express.Router();
Categorie = require('../models/categorie');
router.get('/categories' , (req, res) => {
console.log(" called but the result is: "+Categorie.getTest); //MARKB
res.json({ "test" : Categorie.getTest });
});
module.exports = router; //> imported by the main (node) app.js
Categorie.js
let connection = require('../connection/db');
function getTest() {
let answer = 0;
connection.connectDatabase.query('SELECT 1 + 1 AS solution', (err, rows, fields) => {
if (err){
throw err
};
answer = (rows[0].solution);
});
return answer;
}
module.exports = {
getTest: getTest()
};
I know for sure that the connection itself isn't the problem. Because when running the express app.js the function getTest(), runs and shows the result by the console.log.
But when I call the .../api/categories. I get an JSON response of: { " test ": 0 }
And the same goes for the MARKB console.log
I'm pretty sure that I call the getTest function in the wrong way... but I really can't find an alternative method and hope that someone on Stack Overflow can help me.
The code of the app.js and db.js is probably not the reason but if it can help, here it is:
App.js
const express = require('express');
let bodyParser = require('body-parser');
const http = require('http');
const app = express();
app.use(bodyParser.json());
// Setup header control
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
})
// app.use('/db', require('./connection/db.js')); --> not necessary now
app.use('/api', require('./controllers/categoriesController'));
app.listen(8124, "127.0.0.1");
console.log("Express server listening on port 8124, 127.0.0.1");
Db.js
let mysql = require('mysql');
let settings = require('../settings.json');
let db;
function connectDatabase() {
if (!db) {
db = mysql.createConnection(settings);
db.connect((err) => {
if(!err) {
console.log('Database is connected!');
} else {
console.log('Error connecting database!');
}
});
}
return db;
}
module.exports = {
connectDatabase: connectDatabase()
};
I Really hope someone can help me. Thanks in advance !!