I am beginner in express, js and node.js
I have followed lot of tutorials about express, and i dont understand why the index.html file dont call the corresponding CSS file:
my server.js file:
JavaScript
x
17
17
1
const path = require('path')
2
const express = require('express');
3
4
var app = express();
5
6
app.use(express.static('public'));
7
8
app.get('/0', (req, res) => {
9
res.sendFile('index.html', {root : __dirname });
10
})
11
12
var server = app.listen(8081, function () {
13
var host = server.address().address
14
var port = server.address().port
15
console.log("Example app listening at http://%s:%s", host, port)
16
})
17
my index.html file:
JavaScript
1
18
18
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7
<link rel="stylesheet" href="/public/css/style.css">
8
<title>Pt02 Ch02 Exercise 200</title>
9
</head>
10
<body>
11
12
<div class="container">
13
<div class="heading">Aidez-moi à arrêter de crier !</div>
14
<div class="heading">Je veux rester en majuscules !</div>
15
</div>
16
17
</body>
18
my CSS file:
JavaScript
1
25
25
1
@import url('https://fonts.googleapis.com/css?family=Montserrat:100,900i');
2
html, body {
3
font-family: 'Montserrat', sans-serif;
4
height: 100%;
5
margin: 0;
6
padding: 0;
7
box-sizing: border-box;
8
background-color: blue;
9
}
10
11
.container {
12
height: 100%;
13
align-items: center;
14
}
15
16
.heading {
17
width: 100%;
18
padding: 1.5rem 2.5rem;
19
margin-bottom: 1.5rem;
20
background: #15DEA5;
21
color: #FFF;
22
font-size: 2rem;
23
text-transform: uppercase;
24
}
25
and the different folders of the project:
when i type localhost:8081/0
, the result is not the right result, the CSS is not active.
What am i doing wrong? thanks for help
Advertisement
Answer
express.static bind public
folder to site root (/
) so after that you need to include static files without public
in path:
JavaScript
1
2
1
<link rel="stylesheet" href="/css/style.css">
2