-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
12,985 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
*.rar | ||
*.tar | ||
*.zip | ||
*.tgz | ||
|
||
# Logs and databases # | ||
###################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nodejs 16.13.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.