Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DevSaheerHost/f595827fe31b2b32eaf686c10ad03df0 to your computer and use it in GitHub Desktop.
Save DevSaheerHost/f595827fe31b2b32eaf686c10ad03df0 to your computer and use it in GitHub Desktop.
Server Side CSS example
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