Search code examples
node.jsrouteskong

Kong API Gateway can't route to my API in Node.js


I’m new to the world of API gateways. I’m trying to use an API created in Kong and make my Node.js server (server.js) call this API which is handled by another Node.js program (db_addon.js) to retreive data from a DB. Even reading the documentation I didn’t understand the nature of my problem.

Kong is hosted on 192.168.20.235 (with default configurations) and the Node.js db_addon.js service is on 192.168.20.242 (listening to port 3002). So I created in Kong Manager a service on port 3002 with a path “/api/v1/” and a linked route with path “/getordersdb”. Then, in the server.js, I do a fetch to “http://192.168.20.235:8002/api/v1/getordersdb” which returns a HTTP 200 OK (so I suppose that the API is well created), but in the service program db_addon.js the service is never called. He is waiting the GET API call from http://192.168.20.235:8000/api/v1/getordersdb, but the service is never routed from Kong and I can’t understand why. Am I missing something?

server.js:

const express = require("express");
const cors = require('cors');

const app = express();
const port = 3001;

app.use(cors());

app.get("/api/getorders", async(req, res) => {  //API chiamata da React mediante fetch
  //invocare mediante Kong una API getOrdersDB
  try {
    // Chiamata a Kong API Gateway
    fetch("http://192.168.20.235:8002/api/v1/getordersdb", {
      method: 'GET'
    })
    .then((response) => {
      if (response.ok) {  //HTTP OK
        const orders = response.data;

        console.log("Ordini correnti nel database:\n");
        console.log(orders);

        // Invia la risposta al frontend React
        res.json({ orders });
      }
      else {
        console.error("Errore nella chiamata a Kong API Gateway");
      }
    });
  } catch (error) {
    console.error('Errore durante la chiamata a Kong API Gateway:', error.message);
    res.status(500).json({ error: 'Errore interno del server' });
  } 
});

app.listen(port, () => {
  console.log(`Server avviato su http://localhost:${port}\n`);
});

db_addon.js:

const express = require('express');
const mysql = require("mysql2"); // Importa il modulo MySQL

const app = express();
const port = 3002;

// Configura le informazioni di connessione al database
const db = mysql.createConnection({/*DB CONNECTION INFO*/});

// Connessione al database
db.connect((err) => {
    if (err) {
      console.error("Errore di connessione al database:", err);
    } else {
      console.log("Connesso al database MySQL/MariaDB\n");
    }
});

//API KONG
app.get('/api/v1/getordersdb', (req, res) => {
    console.log("Eseguo la query");
    try {
        db.query("SELECT * FROM orders", async(err, results) => {
            if (err) {
                console.error("Errore durante la query al database:", err);
                res.status(500).json({ error: "Errore durante la query al database" });
            } else {
                console.log("Visualizzazione degli ordini:");
                console.log(results);
                // Invia i risultati a Kong
                res.json(results);
            }
        });
    } catch (error) {
        console.error('Errore generale:', error.message);
        res.status(500).json({ error: 'Errore generale' });
    }
});

app.listen(port, () => {
    console.log(`Server avviato su http://localhost:${port}\n`);
});

Could someone help me solve it please?


Solution

  • Port 8002 is the Kong Manager GUI port. This is not what you want to be calling.

    Kong proxy is running on port 8000 for http by default. So, if you want to get to your api via the Kong proxy, you need to call http://192.168.20.235:8000/api/v1/getordersdb and not http://192.168.20.235:8002/api/v1/getordersdb.

    If you are not able to get the expected response via http://192.168.20.235:8000/api/v1/getordersdb, check the strip_path config attribute of your route which is true by default.

    In a nutshell:

    If your service.path=/api/v1, route.path=/getordersdb and strip_path=true, when you call http://192.168.20.235:8000/api/v1/getordersdb, Kong will proxy the request to http://192.168.20.242:3002/api/v1 (/getordersdb gets stripped)

    If your service.path=/api/v1, route.path=/getordersdb and strip_path=false, when you call http://192.168.20.235:8000/api/v1/getordersdb, Kong will proxy the request to http://192.168.20.242:3002/api/v1/getordersdb

    For more information on how path handling works, you can refer to the official documentation:

    https://docs.konghq.com/gateway/latest/admin-api/#path-handling-algorithms