1

I have this rest API on nodejs as follows

 router.route('/api/Customers')          
    .post(function(req, res) {        
        var Customer = new Customer();
        Customer.name = req.body.name;

        Customer.save(function(err) {
            if (err)
                res.send(err);
            res.json({ message: 'Customer created!' });
        });
  })
    .get(function(req, res) {
        Customer.find(function(err, Customers) {
            if (err)
                res.send(err);

            res.json(Customers);
        });
  });

  router.route('/api/Customers/:Customer_id')    
    .get(function(req, res) {
        Customer.findById(req.params.Customer_id, function(err, Customer) {
            if (err)
                res.send(err);
            res.json(Customer);
        });
    })

    .put(function(req, res) {
        Customer.findById(req.params.Customer_id, function(err, Customer) {
            if (err)
                res.send(err);
            Customer.name = req.body.name;  
            Customer.save(function(err) {
                if (err)
                    res.send(err);
                res.json({ message: 'Customer updated!' });
            });
        });
    })

    .delete(function(req, res) {
        Customer.remove({
            _id: req.params.Customer_id
        }, function(err, Customer) {
            if (err)
                res.send(err);

            res.json({ message: 'Successfully deleted' });
        });
});

How can I create endpoints for specific fields ? For example if I want to GET results for CustomerName, CustomerZip, etc .. Do I have to create separate end points for each field?

1
  • You really have two choices. 1. Have one endpoint return multiple fields. 2. Have separate endpoints return separate fields. Your response object can have any data sent you would like sent from your database, so you can decide what you want each endpoint to return (either 1 field, multiple fields, or no fields at all) Commented May 25, 2016 at 16:08

1 Answer 1

3

Are you using express.js as framework? In this case you can put optional params in your route, for example:

router.route('/api/Customers/:Customer_id?')          
    .post(function(req, res) {        
...
  })
    .get(function(req, res) {
...
  });
});

in this way :Customer_id will be optional and you can manage logic inside your route.

This is a working example:

app.route('/test/:param1?/:param2?')
    .get( function(req, res, next) {

        res.json({
            'param1' : req.params.param1,
            'param2' : req.params.param2
        });

    });

app.listen(8080);

this route supports:

  • /test
  • /test/1
  • /test/1/2

inside response you can see value of this params, I don't know how pass only param2 without param1.

Sign up to request clarification or add additional context in comments.

2 Comments

How can i put multiple parameters here ? And while calling the endpoint should i pass nulls for optional parameters ?
I added a working example, i hope i answered your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.