Skip to content

Instantly share code, notes, and snippets.

@halfnibble
Created February 3, 2024 18:22
Show Gist options
  • Save halfnibble/0a186b2eb245dc86201bbd02c9dce6ed to your computer and use it in GitHub Desktop.
Save halfnibble/0a186b2eb245dc86201bbd02c9dce6ed to your computer and use it in GitHub Desktop.
rainbow-routes/index.js
const express = require('express');
const app = express();
app.get('/:color?', (req, res) => {
let myColor = req.params.color;
console.log('myColor is: ', myColor);
if (typeof myColor === 'undefined') {
// Set to default when undefined.
myColor = 'white';
}
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.navbar {
width: 100%;
height: 60px;
background-color: ${myColor};
text-align: center;
border-bottom: 1px solid #000;
}
.navbar h2 {
margin-top: 0;
padding: 15px;
}
main {
padding: 20px;
}
h1 {
color: ${myColor === 'white' ? 'black' : myColor};
}
</style>
</head>
<body>
<nav class="navbar">
<h2>Navbar</h2>
</nav>
<main>
<h1>${myColor} Page</h1>
</main>
</body>
</html>
`);
});
app.listen(3000, () => {
console.log('Server is running on port http://localhost:3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment