JavaScript Custom URL for REST API in Nodejs + mongodb

So I have the following mongoose schema in the nodejs api:

var profileschema = new mongoose.Schema({
    name:           { type: String },
    surname:        { type: String },
    id:             { type: String }
});

And the following route:

 var profile = express.Router();

 profile.route('/profile')
      .get(profilecrtl.findAllProfile)
      .post(profilecrtl.addProfile); 

I can make another routes like /profile/:id and they work perfectly.

But I would like to generate custom URLs depending on what parameters the users asks and wants on the same method without having to code each case. For example:

  • /profile?id=1234 should give me the full info about the id=1234 profile

{ 
  id: '1234',
  name: 'john'
  surname: 'wicked'
}

  • /profile?id=1234&name=john should give me the same full profile as before

{ 
  id: '1234',
  name: 'john'
  surname: 'wicked'
}

  • /profile?id=1234&fields=name should give me just the name of the id=1234 profile

{ 
  name: 'john'
}

Is there any robust way to do this in the same case so it can easily scale if there is any change in the future?

Answer:1

Since you're using Express, you should use req.query to get an object with the requested arguments.

profile.get('/profile', function (req, res) {
    var query = req.query;
    //Depending on what the query contains, find stuff in your DB with mongoose!
});

req.query is a JSON object containing all of the query arguments of the request, so a request like

/profile?id=1234&fields=name

would result in a JSON object like

{ 
  id: '1234',
  fields: 'name'
}

from which you can create your database queries.

Answer:2

i have the task of setting the border color of inputs fields by error to red using jquery. I did it but i don't wanna be repeating myself. Is there any way to make it shorter and also how to set the ...

i have the task of setting the border color of inputs fields by error to red using jquery. I did it but i don't wanna be repeating myself. Is there any way to make it shorter and also how to set the ...

  1. looping through jquery elements
  2. iterate through jquery array
  3. iterate through jquery elements
  4. iterate through jquery selector
  5. iterate through jquery objects
  6. iterate through jquery
  7. iterate through jquery collection
  8. iterate through jquery results
  9. iterate through jquery list
  10. looping using jquery
  11. iterate through jquery.fn.init
  12. iterate through jquery datatable rows
  13. iterate through jquery class
  14. iterate loop jquery
  15. looping through array jquery
  16. how to loop through jquery datatable
  17. how to loop through jquery
  18. looping through json object jquery
  19. looping through table in jquery
  20. looping through child elements jquery

I have following HTML with two ULs. <a href="javascript:;" id="detail_buttons">Click</a> <ul class="plan none"> <li> <p>One month</p> <...

I have following HTML with two ULs. <a href="javascript:;" id="detail_buttons">Click</a> <ul class="plan none"> <li> <p>One month</p> <...

  1. selected radio button html
  2. selected radio button jquery
  3. selected radio button javascript
  4. selected radio button angular
  5. selected radio button value
  6. selected radio button value jquery
  7. selected radio button css
  8. selected radio button color change
  9. selected radio button value in javascript
  10. selected radio button php
  11. selected radio button android
  12. selected radio button value in php
  13. selected radio button c#
  14. selected radio button in angularjs
  15. selected radio button text jquery
  16. selected radio button id jquery
  17. selected radio button
  18. selected radio button java
  19. selected radio button angular 6
  20. selected radio button default

I want to send Ajax request and perform some unrelated actions while data arrived. After finishing actions I like to wait for Ajax finishing and perform another actions. To be concrete lets see to ...

I want to send Ajax request and perform some unrelated actions while data arrived. After finishing actions I like to wait for Ajax finishing and perform another actions. To be concrete lets see to ...

  1. synchronous function async
  2. synchronous asynchronous function
  3. callback synchronous asynchronous
  4. synchronous asynchronous calls
  5. synchronous asynchronous method
  6. synchronous and asynchronous functions in javascript
  7. synchronous vs asynchronous functions
  8. synchronous and asynchronous functions in c
  9. synchronous vs asynchronous functions javascript
  10. synchronous and asynchronous functions in node js
  11. synchronous and asynchronous functions in autosar

I have a stacked column chart that shows 3 keys / values for each month stacked one on top of the other. Some months may have negatives. The current functionality is for highcharts to put two stacked ...

I have a stacked column chart that shows 3 keys / values for each month stacked one on top of the other. Some months may have negatives. The current functionality is for highcharts to put two stacked ...

  1. highcharts stacked column with line
  2. highcharts stacked column chart
  3. highcharts stacked column colors
  4. highcharts stacked column with drilldown
  5. highcharts stacked column total label
  6. highcharts stacked column multiple series
  7. highcharts stacked column change colors
  8. highcharts stacked column negative values
  9. highcharts stacked column order
  10. highcharts stacked column width
  11. highcharts stacked column sort
  12. highcharts stacked column show percentage
  13. highcharts stacked column with spline
  14. highcharts stacked column datetime
  15. highcharts stacked column chart colors
  16. highcharts stacked column time series
  17. highcharts stacked column click event
  18. highcharts stacked column data labels overlap
  19. highcharts stacked column percentage
  20. highcharts stacked column chart with negative values