Skip to content

Commit

Permalink
hackathon stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiller42 committed Jun 28, 2024
1 parent e017240 commit d3da1d3
Show file tree
Hide file tree
Showing 46 changed files with 7,022 additions and 7,119 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
nodejs 16.20.2
nodejs 16.13.0

83 changes: 83 additions & 0 deletions example-api/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import express, { Router } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import chalk from "chalk";

const app = express();

app.use(
cors({
origin: "http://localhost:3000",
})
);
app.use(bodyParser.json());
app.use(logMiddleware);

const v1 = Router();
v1.get("/:species(dogs|cats)/breeds/list/all", handleRoute);
v1.get("/:species(dogs|cats)/breeds/:breed/images", handleRoute);
v1.post("/:species(dogs|cats)/breeds", handleRoute);

const v2 = Router();
v2.get("/:species(dogs|cats)/breeds/list/all", handleRoute);

app.use("/v1", v1);
app.use("/v2", v2);

/**
* @param {express.Request} req
* @param {express.Response} res
* @param {express.NextFunction} next
*/
function logMiddleware(req, res, next) {
console.log(
`${chalk.bgCyan(`[${localDate(new Date(), true)}]`)} ${chalk.yellow(
req.method
)} ${chalk.magenta(req.url)}`
);
next();
}

/**
* @param {express.Request} req
* @param {express.Response} res
* @returns {void}
*/
function handleRoute(req, res) {
const now = new Date();

res.json({
method: req.method,
path: req.route.path,
params: req.params,
query: req.query,
url: req.originalUrl,
body: req.body,
timestamp_iso: now.toISOString(),
timestamp_local: localDate(now),
});
}

/**
* @param {Date} date
* @param {boolean} [timeOnly = false]
* @returns {string}
*/
function localDate(date, timeOnly = false) {
return date.toLocaleString(["en-US"], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: true,
...(timeOnly
? null
: {
year: "numeric",
month: "2-digit",
day: "2-digit",
timeZoneName: "short",
}),
});
}

app.listen(3333);
22 changes: 22 additions & 0 deletions example-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "example-api",
"type": "module",
"version": "0.0.0",
"scripts": {
"start": "node main.js"
},
"author": "Matt Miller <[email protected]>",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"body-parser": "^1.20.2",
"chalk": "^5.3.0",
"cors": "^2.8.5",
"express": "^4.19.2"
},
"devDependencies": {
"@types/body-parser": "^1.19.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21"
}
}
Loading

0 comments on commit d3da1d3

Please sign in to comment.