Please review my Timestamp Microservice project

Please review my Timestamp Microservice project
0.0 0

#1

Hello fellow Campers, I was very active on November and December getting my Front-end certification, I was not very active during the first months of this year because I have been working a lot. I decided to go to the backend certification, so here is my first project sample. I did not understand the format of the date: December%2015,%202015 so I decided to go with one like this: December%15%2015. I hope you like it, cheers!

https://cyber-clock.glitch.me/May%2015


#2

I found a few glitches. If you do %% instead of % you get a 400 instead of null, null.
If you put a string like December 15, 2015 it gives null null but you mentioned in your post already. While I have done this project, it turns out that %20 is the string you want to split in your reg expression as far as I could see. %20 equels a string like " " with a space in between.

Also you use express which is okay, but could be done without for such a small service.

And as far as I understood it from stackoverflow and previous experience with node.js, the package.json file is just to initialize the dependencies of the server. But in here I could be well wrong. So I wouldnt get the whole package.json file in the server.js. But I don’t know about it. So I was wondering if you have a reason for it?


#3

@Molen thanks for the feedback. I used express because I am learning it, so I decided to go whit it instead of pure node as I think I am going to develop a lot in express in the future. Thanks for clarify the %20 space url parameter. About the 400 bad response, I have tried to fix it but I have not been able, I can´t get the res.statusCode in my middleware, if I console.log just after app.use(function(req, res, next) … I do not get anything, do you know how to fix this? I mean handle those 400 responses from the client?

As to the package json file, I leave it there because I think is necessary to run the script:
“start”: “node timeStampMicroService.js”,
But I am not sure about it as I am new to this.

Thanks for the feedback, I really appreciate it.


#4
[quote]var regExpr = /%/;
date = date.split(regExpr);
var date1 = new Date(date[0] + " " + date[1] + "," + date[2]);[/quote]

It has to do with those lines. The regExpr should be %20. And if there are in this case %% the date object is split in a length of 4. Which gives an invalid new Date object.

So you could try to build an if statement around it and say that when length of the date object is not 3 the response json is null, null.


#5

Hello @molen, I fixed the %20 reg expression and now you are able to put the url: May 20, 2018 or May%2020,%202018, but I am having trouble with the 400 bad request response. I added a console.log(req.url); in line 22, just after the the middleware app.use function. If I add two %% in the url, the req.url becomes “/favicon.ico”, so I am not able to get the URL to apply the fix you proposed. I have searched all over the web and have not found any solution yet. I will ask this question in stackoverflow too because I want to get rid of that 400 response.


#6

May%2013,%20%202015

This date gives now problems. I suggest you to put a console.log(date) to see the problem and make your regular expression a little better. Also I suggested to put an if statement with date.length == 4 after you split the date. It looks a little redunded with the if chain. But it will work in the end.

Line 26, 27 and below.

date = date.split(regExpr);
console.log(date);

if(date.length == 4){


#7

Express auto decodes/ encodes URI components , so the weird %20 that you see there is an encoding for the space character as that is not allowed for a URI component. So if I directly enter May 05,2018 in the browser it gets automatically encoded to May%2005,2018 and express will decode it back to the original string I entered . So regex was unnecessary for this project.


#8

https://timestamp-micro-serv.glitch.me/september%2022,2017%

Your solution also doesn’t work for a wrong date like this. I like the code.


#9

I don’t understand, exactly what type of date input does it not work for you ?
If the string input can be parsed with javascript’s builtin Date.parse() method then it will work, if it can’t then it will return null for both properties . The method works for typical iso string date formats, which is enough to cover for all the user stories of this project.


#10

Part 6 of the user story/assignment is not working properly.


#11

Interesting case ! that is a malformed URI and can not be decoded with javaScript .So it needs an error handler , which I have updated my old code , the error can only be handled with express’ error handling middleware at the end of the server file.

app.use(function (err, req, res, next) {
  try {
    decodeURIComponent(req.path);
  }
  catch (e) {
    res.send({"unix":null,"natural":null,"error":e.message})
  }
})

HOWEVER, while this works locally it does not work on glitch , because the glitch server throws the error before it even gets to the app

The sample project is hosted on heroku and also gives a bad request error for that input as well. So moral of the story, don’t send a malformed URI, or you could check on the client side before sending to the server for a real world application for instance. Interesting anyhow


#12

Thank you for figuring it out!


#13

Hello @Dereje1, thanks for sharing your code.


#14

No problem @juandata, I also wanted to add, in confirmation with what @molen also said earlier, that you do not have to directly read the package.json file in your server.js file or any other part of your application. The package.json file is used to load any dependencies that your application needs only once by the machine that is hosting the application. This is done either through the npm install or yarn command, if additional dependencies are introdiced to the project then those commands would have to be executed again, but only if additional dependencies are added or a version change of a previous package is required. You can learn more about the package.json file here.