Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
¿Cómo puede importar CSS en un servidor node.js?

Digamos que tienes un archivo CSS con el código:

 body{ margin:0px; }

Y el archivo HTML index.html. ¿Cómo podría crear un servidor que tome index.html y agregue el CSS? Editar:

 var http = require('http'); var fs = require('fs'); function sendFile(res, filename, type) { fs.readFile(filename, function(err, data) { if (err) { console.log(err); res.statusCode = 404; res.end(); return ; } res.writeHead(200, { 'Content-Type': type }); res.write(data); res.end(); }); } http.createServer(function(req, res) { if (req.url == "/") { sendFile(res, "index.html", "text/html"); } else if (req.url == "/styles.css") { sendFile(res, "styles.css", "text/css"); } else { res.statusCode = 404; res.end(); } }).listen(8080);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto es mucho más simple con un marco simple como Express, donde puede configurarlo para servir automáticamente un directorio de archivos estáticos.

Pero, si desea hacerlo usted mismo usando solo el objeto http incorporado, puede hacer algo como esto:

 const http = require('http'); const fs = require('fs'); function sendFile(res, filename, type) { fs.readFile(filename, function(err, data) { if (err) { console.log(err); res.statusCode = 404; res.end(); return; } res.writeHead(200, { 'Content-Type': type }); res.write(data); res.end(); }); } http.createServer(function(req, res) { if (req.url === "/") { sendFile(res, "index.html", "text/html"); } else if (req.url === "/styles.css") { sendFile(res, "styles.css", "text/css"); } else { res.statusCode = 404; res.end(); } }).listen(8080);

Luego, dentro de index.html , puedes tener:

 <html> <head> <link href="/styles.css" rel="stylesheet"> </head> <body> <div class="red">This is some text that is supposed to be red</div> </body> </html>

En styles.css , puede tener:

 .red { color: red; }

Eso hará que el navegador solicite /styles.css y su servidor web se configurará para servir ese archivo para esa solicitud.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!