I have an express server in a file server1.js and I have another server in a file server2.js. I would like to know how I can call Server2 getUserId api in the Server1 addUser api?
server1.js
// Server1
const express = require("express");
const app = express();
app.get('/api/addUser/:userName', (req, res) => {
const user = {
userName: req.params.userName,
userId: // call to getUserId api to get userId from server2
};
users.push(user);
res.json(`user addedd: ${JSON.stringify(user)}`);
});
app.listen(3000, () => {
console.log("Listen on the port 3000...");
});
Server2.js
// Server2
const express = require("express");
const app = express();
app.get('/api/getUserId', (req, res) => {
res.json(Math.random());
});
app.listen(3001, () => {
console.log("Listen on the port 3001...");
});