Express: how to send html with css using sendFile?

var app = require('express')();

app.get('/', function(req, res) {
  res.sendFile(__dirname + "/" + "index.html");
});
<link rel="stylesheet" href="style.css">
Run codeHide result

I used the node.js code above to send the html file. To get the html file format, I need to send another css file (style.css).
My question is: how can I send both of these files (index.html and style.css) using sendFile () and merge them together on the client side?

+4
source share
1 answer

The browser must load by style.cssitself, so you can use this as a route:

app.get('/style.css', function(req, res) {
  res.sendFile(__dirname + "/" + "style.css");
});

, , . Express :

https://expressjs.com/en/starter/static-files.html

const express = require("express");
const app = express();
app.use(express.static(__dirname));

, index.html , , , .

index.html, css, , .. , , public :

app.use(express.static("public"));

, Express index.html , app.get("/".

+11

Source: https://habr.com/ru/post/1650102/


All Articles