Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How to use an endpoint as a function inside another function using express js

I want to get the shop if shop does not exist then call app.post() to add a new shop.

My app.post() it creates the shop successfully:

app.post('/shops/addShop', (req, res) => {
      const shop = {
        id: 'shop' + (shops.length + 1),
        name: req.body.name,
        departmentList: [
        {
          id: 'd1',
          name: "department 1"
        },
        {
          id: 'd2',
          name: "department 2"
        },
        {
          id: 'd3',
          name: "department 3"
        }
        ]
      };
   
      shops.push(shop);
      res.send(shop);
});

How to implement getShop() by Id if exist otherwise create it by using app.post():

app.get('/shops/:id', (req, res) => {
   // how to code here
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You shouldn't call app.post() directly, that's an API controller, instead you want to have a separate function that performs this logic.

app.post('/shops', (req, res) => {
  res.send(createNewShop());
});

app.get('/shops/:id', (req, res) => {
  const shopExists = findShop(req.params.id);

  if (!shopExists) {
    return res.send(createNewShop());
  } else {
    // do something with your shop
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!