Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

Node.js REST API to call other apis to push data to external systems

I am building an portal for our company for which the backend is a Node.js(Express) REST api using sequelize ORM and SQL Server database.

Initially the application was simple and users would do actions in the frontend to perform GET/POST/PUT/DELETE operations on the backend api. So I would have CRUD API with routes like below

import { Router } from 'express';
import { CarrierController } from '../controllers/carrier';

const carrierRoutes = Router();
const carrierController = new CarrierController();

// Get All Carriers
carrierRoutes.get('/' , carrierController.getAllCarriers);
// Get Single Carrier by ID
carrierRoutes.get('/:id' , carrierController.getSingleCarrier);
// Create a new carrier
carrierRoutes.post('/' , carrierController.createCarrier);
// Update a carrier
carrierRoutes.put('/:id', carrierController.updateCarrier);
// Delete a carrier by id
carrierRoutes.delete('/:id', carrierController.deleteCarrier);

export default carrierRoutes;

And the controller would perfrom the necessary action with the DB using sequelize as seen below:

import models from '../models';
import Util from '../Utils/Utils';
import { Request, Response  } from "express";
import { DestroyOptions, UpdateOptions } from 'sequelize';
// import { Carrier, CarrierInterface } from '../models/carrier';
// import { DestroyOptions, UpdateOptions } from 'sequelize/types';

const util = new Util();

export class CarrierController {
 
    // Create a new carrier
    public async createCarrier(req: Request, res: Response) {
        const newCarrier = req.body;
        try {
        const createdCarrier = await models.carrier.create(newCarrier);
        util.sendResponse(201,createdCarrier,res);
        } 
        catch (error) {
          util.sendResponse(500,{message: error.message,},res);
        }
    }
}

Now the problem is I am integrating with other systems where I have to push data to other systems via rest api to send them carrier information. Is it a good practice to be calling 3 furthur rest api's on a rest api endpoint?

Am currently thinking of implementing it with the following way in the controller:

public async createCarrier(req: Request, res: Response) {
  const newCarrier = req.body;
  try {
    // First pushing the data to our database using sequelize create method
  const createdCarrier = await models.carrier.create(newCarrier);


  const system1Endpoint = process.env.system1EndpointUrl;
  const system2Endpoint = process.env.system2EndpointUrl;
  const system3Endpoint = process.env.system3EndpointUrl;

  // Pushing Data to 3 other partners via there apis
  const system1Data = await axios.post(system1Endpoint, newCarrier);
  const system2Data = await axios.post(system2Endpoint, newCarrier);
  const system3Data = await axios.post(system3Endpoint, newCarrier);

  // Finally send a response back 
  util.sendResponse(201,createdCarrier,res);
  } 
  catch (error) {
    util.sendResponse(500,{message: error.message,},res);
  }
}

Is this a right way of achieving what I want to achieve? Because I feel that it may not be best practice to do it this way. Because firsly I want to push data to all other applications simultanously at the same time in the current one it is doing 1 at a time. The second problem is it is potentially slowing my api for my users on the client side when they perform these actions because pushing to other apis will take their time too.

Please let me know if there is a better way of doing this and how it can be achieved in Node.js, Express.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...