Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamid6426 authored Jul 23, 2024
1 parent 7db74e0 commit 9ac73ff
Show file tree
Hide file tree
Showing 39 changed files with 6,814 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 001-timestamp-microservice/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Mian Hamid Ur Rehman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions 001-timestamp-microservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Timestamp-Microservice

This is the boilerplate code for the Timestamp Microservice project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/timestamp-microservice
53 changes: 53 additions & 0 deletions 001-timestamp-microservice/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// index.js
// where your node app starts

// init project
var express = require('express');
var app = express();

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC
var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200})); // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (req, res) {
res.sendFile(__dirname + '/views/index.html');
});

const isInvalidDate = (date) => date.toUTCString() === "Invalid Date"

// your first API endpoint...
app.get("/api/:date", function (req, res) {
let date = new Date(req.params.date)

if(isInvalidDate(date)){
date = new Date(+req.params.date)
}

if(isInvalidDate(date)){
res.json({error: "Invalid Date"})
return;
}

res.json({
unix: date.getTime(),
utc: date.toUTCString()
});

});

app.get("/api", (req, res) => {
res.json({
unix: new Date().getTime(),
utc: new Date().toUTCString()
})
})

// Listen on port set in environment variable or default to 3000
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
Loading

0 comments on commit 9ac73ff

Please sign in to comment.