node-express how to pass DATE params in URL query string and how to parse that

how to pass date parameter in url c#
how to pass date parameter in url web api
express get query params
node js http request get parameters
pass date as query param
pass date value in query string
how to pass date in get request
how to pass date as parameter in postman

I have an app built on angular 2, a service in that sending http request to fetch the data from oracle DB, usind node-oracle db and express framework. I have built rest api's using express, now i need to pass the DATE in request parameter and express has to parse that and send the response. How can i pass the DATE in query param and how i can parse that in express rest api.

pass the date with iso format 'yyyy-mm-dd'

const date = new Date();
http.get(`url/test?date=${date.toISOString()}`

in the express side

app.get(/test', async function(req, res) {

const dateInServer = newDate(req.query.date);

});

Query String, I have built rest api's using express, now i need to pass the DATE in request parameter and express has to parse that and send the response. How can i pass the� In this article I presented ways to extract both the query string parameters and route path parameters a URL in the Express web framework. Here is a quick recap of how to extract the parameters we talked about: req.query: directly access the parsed query string parameters; req.params: directly access the parsed route parameters from the path

Dates are one of only javascript types that don't get stored when you Stringify an object.

You can see Issues with Date() when using JSON.stringify() and JSON.parse() for more info.

Your options here are either:

Split the date on input

If you are only looking for a date you can split it into 3 parameters

var valueToSend = {
  date: {
    day: date.getDate(),
    month: date.getMonth(),
    year: date.getYear()
}

Then on the express side

new Date(req.body.year, req.body.month, req.body.date)

The Pros of this approach is that is is easy to validate and you are only sending the info you need. The downside is that it is more code

Use Regex on the express side

You could make a middleware that tests for a date formatted string and convert it to a date using JSON.parse reviver function as the second parameter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

e.g.

 module.exports = (req, res, next) => {
     for (var bodyKey in req.body) {
         if (req.body.hasOwnProperty(bodyKey)) {
             req.body[bodyKey] = JSON.parse(req.body[bodyKey],dateTimeReviver);
         }
     }
     next();
 };

function dateTimeReviver(key, value) {
  var a;
    if (typeof value === 'string') {
        a = /[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z/.exec(value);
        if (a) {
            return new Date(a[0]);
        }
    }
    return value;
}

Get Query Strings and Parameters in Express.js, escape() method performs URL percent-encoding on the given str in a manner that is optimized for the specific requirements of URL query strings. The querystring. In Express, use req.query.. req.params only gets the route parameters, not the query string parameters. See the express or sails documentation: (req.params) Checks route params, ex: /user/:id

I wrote a series on dates that covers what happens as they move between clients, mid-tiers, and Oracle Database. Here's the part that touches on node-oracledb. You might find some of that info useful.

Peter's answer already covers parsing the date from an ISO 8601 string that comes from the client after being stringified. I'll add that you want to make sure the correct time zone when connecting to Oracle if the date is being inserted into DATE or TIMESTAMP columns. The doc covers this as well.

Node/Express Timestamp : Using req.query to grab date and turn it , In this article, I assume you have some experience with Node.js and creating Express.js In simple terms, a query string is the part of a URL (Uniform Resource of the URL parsing for you and exposes the retrieved parameters as this object. In Express, use req.query.. req.params only gets the route parameters, not the query string parameters. See the express or sails documentation: (req.params) Checks route params, ex: /user/:id

How to retrieve the GET query string parameters using Express, function (req, res, date) { // If query params are empty use the current date Node/Express Timestamp : Using req.query to grab date and turn it into statement I am grabbing 2015-12-25 from the URL via req.query . If you want to get the data through route parameters: It won't let me pass it like this: Often when we are building applications using ExpressJS, we will need to get information from our users. This can happen any number of ways but the two most popular are: URL Parameters These are information that are passed through the URL

API Reference, The query string is the part that comes after the URL path and starts with a Retrieve the GET query string parameters using Express Download my free Express.js Handbook, and check out my premium Node.js Course. query parser. Varied: Disable query parsing by setting the value to false, or set the query parser to use either “simple” or “extended” or a custom query string parsing function. The simple query parser is based on Node’s native query parser, querystring. The extended query parser is based on qs.

Building a simple API With Node.js & JavaScript Dates, A new body object containing the parsed data is populated on the request lastModified, Set the Last-Modified header to the last modified date of the file on the OS. the maximum number of parameters that are allowed in the URL- encoded data. The consolidate.js library maps Node template engines to follow this� JSON.stringify(new Date()) Results in a string. JSON does not have a primitive representation of Date objects that JSON.parse will turn back into a Date object automatically. The Date object's constructor can take a date string, so you can turn those string values back into dates by doing: var x = new Date(JSON.parse(JSON.stringify(new Date())));

Comments
  • use new Date(req.query.date) instead of newDate(). Otherwise this is fine.
  • I have to send the date along with time
  • Then option 2 would be better, but you could send `{ date: { day: date.getDate(), month: date.getMonth(), year: date.getYear() }
  • Then the second option might be better. Just need to change the express side