Can someone tell me exactly what the last two lines of the sample code below are doing?
var express = require('express')
var app = express();
var http = require('http').Server(app);
var port = process.env.PORT || 8080;
var moment = require('moment');
app.get('/:date', function (req, res) {
var queryDate = new Date(req.params.date);
app.get('/:date', function (req, res) {
var queryDate = new Date(req.params.date);
app.get('/')
gets a route. So app.get('/')
would get localhost:3000
while app.get('/api')
would get localhost:3000/api
. The :date
is a parameter. So you are getting this route: localhost:3000/january5th
, localhost:3000/bruh
, localhost:3000/4thofjuly
. The part after the /
is considered a request parameter with the name date
. You can access this with req.params.date
. In the above case, req.params.date
would be : “january5th”, “bruh”, “4thofjuly”. Obviously it is up to you to make sure it is a valid date before doing new Date(req.params.date)
You can read more about routing and parameters here:
http://expressjs.com/en/guide/routing.html
3 Likes
That was the link I was searching for 
1 Like
I see. I could actually name that anything I want and have it still work when I feed the service a date.
Using “https://firstappever-olddognewtrix123.c9users.io/December%2015,%202015” if I use the following code
app.get('/:samIam', function (req, res) {
var queryDate = new Date(req.params.samIam);
I get “Tue Dec 15 2015 00:00:00 GMT+0000 (UTC)”
But if there is a mismatch, such as
app.get('/:samIam', function (req, res) {
var queryDate = new Date(req.params.date);
stuff stops working and the output is “Invalid Date”