I created super simple express app which will display one home.html view from view directory. Everything is ok home.html
is displayed but there is no any css styles that i added. In the console i get only one error:
I tried to solve my problem with another answers from stack that I found like:
- change app.use()
- from ->
app.use(express.static(path.join(__dirname, 'public')))
- to ->
app.use(express.static(path.join(__dirname + '/public')))
- from ->
Okay here you can see how my directory structure looks like
As you can see I created public folder and inside of this folder i keep my css styles, so under the url: http://localhost:8080/public/css/style.css
I should see my css styles but I can see only error
But in the same time under this url http://localhost:8080/css/style.css
I can find my css file
I am really confused and I do not know what I am doing wrong. I will be very glad If any of you help me.
app.js
const express = require('express'); const path = require('path'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); const flash = require('connect-flash'); const routes = require('./routes/index'); const app = express(); app.set('views', path.join(__dirname, 'views')); app.engine('html', require('ejs').renderFile); app.set('view engine', 'html'); app.use(express.static(path.join(__dirname, 'public'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); app.use(flash()); app.use('/', routes); module.exports = app;
server.js
const app = require('./app'); app.set('port', process.env.PORT || 8080); const server = app.listen(app.get('port'), () => { console.log(`Server is up on ${server.address().port}`); });
home.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="/public/css/style.css"> <title>Express</title> </head> <body> <h1>Express</h1> </body> </html>
style.css
body { background: #f6f6f6; } h1 { color: green; }