Timestamp Microservice Help

Hey, The code here isn’t working for this project (Timestamp Microservice for APIs and Microservices). Whenever I refresh all that shows up is this: cannot GET / .
I don’t understand what the problem could be. Could someone please help me out on this? Here’s the code for server.js:

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(“Timestamp”);
});

Here’s the code for package.json:

{ “author”: “Donna Amos”,
“name”: “timestamp-microservices-challenge”,
“version”: “0.0.1”,
“description”: “A time stamp page that converts time to unix and natural”,
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
},
“dependencies”: {
“express”: “^4.12.4”,
“cors”: “^2.8.0”
},
“engines”: {
“node”: “4.4.5”
},
“repository”: {
“type”: “git”,
“url”: “https://github.com/donnacamos/timestamp-microservice-forReal
},
“keywords”: [
“node”,
“hyperdev”,
“express”,
“freecodecamp”
],
“license”: “MIT”
}

Here’s the git repository for this project:

And here’s the glitch link for what shows up:
https://learned-roundworm.glitch.me/

Here’s the glitch link:

Thanks!

You don’t have a “/” route being handled on the back end.


Thanks for your help. I added this line:

res.send(req.params);

To this code:
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.send(req.params);
res.json({unix: unixDate, natural: naturalDate});
});
But I’m still getting the same error. What am I missing? Could you please be more specific? I’m new to back end and find it all very confusing. I understand the principles behind what Node.js and Express are supposed to do. It’s when it comes to implementing them that I have trouble.
Thanks again!

Hi @donnacamos88,

So I looked at your glitch and found the problem. First I will try to break it down so you can find it yourself. To be fair my terminology may not be the best but bear with me.

Basically when the project is loaded the front-end is looking for an index.html but there is not one. Usually you have this in the root folder of your website or glitch project, etc.

You have an index.html file but it is in the /views folder. So it will not automatically load when you go to the root directory of the site.

What @EddieCornelious is referencing is the base route of the site. When your root page is loaded no file is being served. That is what is meant by the '/' route

The same way you are looking to handle the '/dateValues/:dateVal' route you need to handle the base route of your web app which is the '/' route

I hope this helps… It goes something like app.get() where you need to send the index.html file to the route directory of your app when it is called.

Thanks so much @ j8ahmed! I never knew where to find the html section of Glitch before. Thanks for explaining ‘/’ route more thoroughly. I feel silly asking these questions but I’ve been trying on my own for too long. Thanks for being patient with me. Back-end is confusing but I’m starting to get a better picture of it now that you told me where to find the html section. I probably should go back and read these articles from EddieCornelius now that you’ve given me a more thorough explanation. Hopefully I’ll have it solved soon.

1 Like

No problem. I can relate to what you are saying about the transition to Back-end development. I went through it myself. Looking into short videos explaining what MVC (Model View Controller) Frameworks are may help you as well (What they are and why they are used). These concepts are subtly implied in the challenges and projects for the API and Microservices certification.

As long as you gave it your best shot there is nothing wrong with asking for help! Everyone needs a few tips now and then. In the future though, I would suggest using the ``` quotes ``` to contain any code you paste so that it is easier for everyone to read. You just enclose the code in three ``` on each end.

Ex: let code = a + b = c;

What I wrote looks likes this ``` let code = a + b + c; ``` on my end but it appears much better to readers.

All the best moving forward!! I am glad I could help.