Sunday, September 1, 2024

Boilerplate Project NodeJs Express Timestamp

// 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');
});


app.get("/api/:time?", function (req, res) {
  let date;
  if (req.params.time) {
    // Check if it is a Unix timestamp
    if (!isNaN(req.params.time)) {
      date = new Date(parseInt(req.params.time));
    } else {
      date = new Date(req.params.time);
    }
  } else {
    // If no parameter, use the current date and time
    date = new Date();
  }

  if (!isNaN(date.getTime())) {
    // If date is valid, return the Unix timestamp and UTC string
    res.json({unix: date.getTime(), utc: date.toUTCString()});
  } else {
    // If date is not valid, return an error message
    res.json({error: "Invalid Date"});
  }
});

// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});

views/index.html

<!DOCTYPE html>

<html>
   <head>
      <title>Timestamp Microservice | freeCodeCamp.org</title>
      <link rel="shortcut icon" href="https://cdn.freecodecamp.org/universal/favicons/favicon-32x32.png" type="image/x-icon"/>
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
      <link href="style.css" rel="stylesheet" type="text/css">
   </head>

   <body>
    <h1>Timestamp Microservice</h1>
    <hr />
    <div class="container">
      <h3>Example Usage:</h3>
      <ul>
        <li><a href="api/2015-12-25">[project url]/api/2015-12-25</a></li>
        <li><a href="api/1451001600000">[project url]/api/1451001600000</a></li>
      </ul>

      <h3>Example Output:</h3>
      <p>
        <code>{"unix":1451001600000, "utc":"Fri, 25 Dec 2015 00:00:00 GMT"}</code>
      </p>
    </div>
    <div class="footer">
      <p>
        By <a href="https://www.freecodecamp.org/">freeCodeCamp.org</a>
      </p>
    </div>
  </body>
</html>

public/style.css

/****** Main Styling ******/

body {
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    color: #222;
    background-color: #FaFaFa;
    text-align: center;
    line-height: 1.4em;
}a

.container {
    padding: 0;
    margin-top: 40px;
}

h3 {
  margin-top: 30px;
}

hr {
    margin: 25px;
}

.footer {
    margin-top: 40px;
}

code {
  font-family: monospace;
  padding: 2px;
  color: black;
  background-color: #fff;
}

ul {
    list-style-type: none;
}

li {
  margin-bottom: 0.5em;
}

a {
    color: #2574A9;
}

package.json

{
  "name": "fcc-api-projects-boilerplate",
  "version": "0.0.1",
  "description": "An FCC Backend Challenge",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.12.4",
    "cors": "^2.8.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-project-timestamp.git"
  },
  "bugs": {
    "url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
  },
  "homepage": "https://github.com/freeCodeCamp/boilerplate-project-timestamp#readme",
  "author": "freeCodeCamp <team@freecodecamp.org>",
  "keywords": [
    "node",
    "express",
    "freeCodeCamp"
  ],
  "license": "MIT"
}

readme.md

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

Timestamp Microservice Example Usage:

[project url]/api/2015-12-25
[project url]/api/1451001600000

Example Output:

{"unix":1451001600000, "utc":"Fri, 25 Dec 2015 00:00:00 GMT"} app.get("/api/:time", function (req, res) { console.log(req.params.time); let unixtime; if (Date.parse(req.params.time) > 0) { unixtime = new Date(req.params.time).getTime() / 1000; } else { unixtime = req.params.time; } const utctime = new Date(req.params.time).toUTCString(); console.log(utctime); res.json({unix: unixtime, utc: utctime}); });