Timestamp microservice project not working

var express = require(“express”);
var cors = require(“cors”);
var bodyParser = require(“body-parser”);

var app = express();

app.use(bodyParser.json());
app.use(cors());

app.get("/dateValues/:dateVal", function(req,res,next){

var dateVal = req.params.dateVal;

var dateFormatOptions = {
year:‘numeric’,
month:‘long’,
day:‘numeric’
};
if(NaN(dateval)){
var naturalDate = new Date(dateval *1000)
naturalDate = naturalDate.tolocaleDatestring(“en-us”, dateFormatOptions);
var unixDate = new Date(dateVal).getTime()/1000;
}
else
{
var unixdate=dateVal;
naturalDate = naturalDate.tolocaleDatestring(“en-us”, dateFormatOptions);
}

res.json({unix: unixDate, natural: naturalDate});
});

app.listen(3000, function(){
console.log(“booooooo”);
});

this code is not working can u please check the code.

First of all you can avoid the next parameter in the route definition, it helps you differentiate between end points and middlewares.
That said, you have a lot of case sensitive problems and a wrong function name (NaN - isNaN)

Thankyou so much sir.

1 Like

still not working
var express = require(“express”);
var cors = require(“cors”);
var bodyParser = require(“body-parser”);

var app =express();

app.use(bodyParser.json());
app.use(cors());

app.get(’/dateValues/:dateVal’, function(req,res){

var dateVal = req.params.dateVal;

var dateFormatingOptions = {
year:‘numeric’,
month:‘long’,
day:‘numeric’
};
if(isNaN(dateVal)){
var naturalDate = new Date(dateVal *1000)
naturalDate = naturalDate.tolocaleDatestring(“en-us”, dateFormatingOptions);
var unixDate = new Date(dateVal).getTime()/1000;
}
else
{
var unixdate=dateVal;
naturalDate = naturalDate.tolocaleDatestring(“en-us”, dateFormatingOptions);
}

res.json({unix: unixDate, natural: naturalDate});
});

app.listen(3000, function(){
console.log(“i am not going to work”);
});

1 Like

Locally it works for me - I realized that it wasn’t catching a last typo because it’s into the if…else statement: is not tolocaleDatestring, is toLocaleDateString