Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
19 views
ago in Information Technology by (150k points)
Can you provide a simple example of an API Gateway implementation?

Please log in or register to answer this question.

1 Answer

+1 vote
ago by (150k points)

Here's a basic example using Node.js and Express:

Step 1: Install dependencies

npm install express http-proxy-middleware

Step 2: Create gateway.js

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();

// Service Proxies
app.use('/service1', createProxyMiddleware({ target: 'http://localhost:3001', changeOrigin: true }));
app.use('/service2', createProxyMiddleware({ target: 'http://localhost:3002', changeOrigin: true }));

// Start the Gateway
app.listen(3000, () => {
    console.log('API Gateway running on http://localhost:3000');
});

Step 3: Create services

Create service1.js and service2.js to represent two different backend services.

service1.js

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Response from Service 1');
});

app.listen(3001, () => {
    console.log('Service 1 running on http://localhost:3001');
});

service2.js

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Response from Service 2');
});

app.listen(3002, () => {
    console.log('Service 2 running on http://localhost:3002');
});

Step 4: Run the services and the gateway

Open three terminal windows and run the following commands:

Terminal 1:

node service1.js

Terminal 2:

node service2.js

Terminal 3:

node gateway.js

Now, you can access the services via the API Gateway:

  • http://localhost:3000/service1
  • http://localhost:3000/service2

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...