How to call Api in Node js

5 Easy Ways to Make HTTP Requests in Node.js Follow Under Steps By step

1: users.routes.js

app.post("/api_name", [
                   check('PARMS NAME').not().isEmpty().trim(),
                   check('PARMS NAME').not().isEmpty().trim(),
], users.ROUTE_NAME);

2: users.controller.js

exports.ROUTE_NAME= (req, res) => {

// Validate request
               const errors = validationResult(req);
               if (!errors.isEmpty()) {
               return res.status(422).json({errors: errors.array()});
               }   
          ObjInfo = {
                   user_id: req.body.user_id,
   };
   users.MDLROUTE_NAME(req, (err, INFO) => {
          if (err) {
                 res.send(err);
          } else {
                 resData = {};
                 resData.info = INFO;
                 resData.status = 1;
                 res.send(resData);
          }

   });

};

3: users.model.js

Follow this model route name called is..

users.MDLROUTE_NAME= (req, result) => {   
   var user_id = req.body.user_id;

   sql.query("$sql_query", [key_name], (err, res) => {
          if (err) {
                 console.log("error: ", err);
                 result(null, err);
                 return;
          } else {
                 result(null, res);
          }
   });
};

i hope it can help you…

Leave a Comment