0

I have attached my css file to my html file. And then i run and open page using express in node js. However, the css file does not open when i run the webserver through node js.
html(show.ejs)

<html>
<head>
<link rel="stylesheet" type="text/css" href="assets/css/style.css" media="screen" />
</head>
  <body>
    <h1> Value is <%= detail %></h1>
  </body>
</html> 

node js

//required npm
var express = require('express');//express 4.*

var path = require('path');

// define app
var app = express();

// set up template engine
app.set('view engine', 'ejs');


//static files
//app.use('/static', express.static('/public')); //not working
app.use('', express.static(path.join(__dirname, 'public')));  //not working
//app.use(express.static(__dirname + '/public'));  //not working
//app.use('/public/assets', express.static('public/assets'));  //not working

app.get('/show/:id', function (req, res) {
    res.render('./panel/show', {
        detail: req.params.id ,
    });

//port
app.listen(3000);

my project folder

node_modules

views

panel

show.ejs

public

assets

css

style.css

app.js

package.json

8
  • yes, but this is not worked href="assets/css/style.css" Commented Mar 16, 2018 at 5:44
  • are you getting 404? Commented Mar 16, 2018 at 5:45
  • no, css not finding. in inspect element style.css path is "localhost:3000/show/asset/style.css" Commented Mar 16, 2018 at 5:46
  • 1
    @JavadDehhabe What is the error? Commented Mar 16, 2018 at 5:51
  • style.css not loading in page. when click href="assets/css/style.css" the page error "Cannot GET /show/assets/css/styles.css" Commented Mar 16, 2018 at 5:58

1 Answer 1

1

By entering <link rel="stylesheet" type="text/css" href="assets/css/style.css" media="screen" /> You are trying to find the assets folder in your out of public directory.

So, when you / it will find public directory which is statically defined in express server.

<html>
<head>
<link type="text/css" href="/assets/css/styles.css" rel="stylesheet">
</head>
  <body>
    <h1> Value is <%= detail %></h1>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.