Created
August 14, 2024 06:21
-
-
Save DevSaheerHost/f595827fe31b2b32eaf686c10ad03df0 to your computer and use it in GitHub Desktop.
Server Side CSS example
This file contains hidden or 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
dddHere's an example of server-side CSS using Node.js and Express.js: | |
*server.js* | |
``` | |
const express = require('express'); | |
const app = express(); | |
// Define a CSS stylesheet as a string | |
const stylesheet = ` | |
body { | |
background-color: #f2f2f2; | |
} | |
h1 { | |
color: #00698f; | |
} | |
`; | |
// Serve the CSS stylesheet | |
app.get('/styles.css', (req, res) => { | |
res.type('css'); | |
res.send(stylesheet); | |
}); | |
// Serve an HTML page that links to the CSS stylesheet | |
app.get('/', (req, res) => { | |
res.send(` | |
<html> | |
<head> | |
<link rel="stylesheet" href="/styles.css"> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
</body> | |
</html> | |
`); | |
}); | |
app.listen(3000, () => { | |
console.log('Server started on port 3000'); | |
}); | |
``` | |
In this example, we define a CSS stylesheet as a string and serve it from the `/styles.css` endpoint. We then serve an HTML page from the root endpoint (`/`) that links to the CSS stylesheet. | |
When you run this server and navigate to `http://localhost:3000` in your browser, you should see an HTML page with a styled heading that says "Hello World!". | |
Note that this is a very basic example, and in a real-world scenario, you would likely want to use a more robust CSS preprocessor like Sass or Less, and possibly even a CSS framework like Bootstrap or Tailwind CSS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment