I have the following URL:
https://electric-stoat.glitch.me/api/timestamp/:date_string?2015-12-25
In my Express app.get
function I want to grab 2015-12-25
and return JSON that looks like this:
{unix: < The UNIX timestamp for 2015-12-15 in milliseconds >, utc: < The UTC timestamp for 2015-12-15 >}
How can I grab 2015-12-15
from req.query
and turn it into a UNIX timestamp and a UTC timestamp? Here is what I have so far, ignore the first part of the if statement. In the else I want req.query
to be turned into UNIX/UTC timestamps.
app.get("/api/timestamp/:date_string?", function (req, res, date) {
// If query params are empty use the current date
if(Object.keys(req.query).length === 0) {
date = Date.now();
console.log("Date.now outputs : " + date);
var dateUTC = new Date().toUTCString();
res.json({
unix: date,
utc: dateUTC
});
}
// If query params are provided use date provided by req.query
else {
res.json({
unix: req.query,
utc: req.query
})
}
req.params contains route parameters (in the path portion of the URL), and req.query contains the URL query parameters (after the ? in the URL).
You can also use req.param(name) to look up a parameter in both places (as well as req.body), but this method is now deprecated.