简体   繁体   中英

Node.js Loading css

My folders:

server.js
index.html
-public
 --css
  --ses.scss
  --style.css

Server js:

var express = require('express');
    var path = require('path');
    var publicPath = path.resolve(path.join(__dirname, 'public'));
    var app = express();
    var http = require('http').Server(app);
    var fs = require('fs');

    app.use(express.static(path.join(__dirname, 'public')));
    app.get('/', function (req, res) { 
        res.sendfile('./index.html');

    });
http.listen(3000, function () {
    console.log('listening on *:3000');
});

Index.html:

<link rel="stylesheet" type="text/css" href="/public/css/ses.scss">
<link rel="stylesheet" type="text/css" href="/public/css/style.css">

CSS works when I open index.html with google chrome without server.When I use node.js CSS isn't loading.

express.static() serves the contents of its folder directly on the root.

Therefore, public/ is not part of the URL.

Node :

app.use("/css",express.static(path.join(__dirname, 'public/css')));

css :

<link rel="stylesheet" type="text/css" href="/css/ses.scss">
<link rel="stylesheet" type="text/css" href="/css/style.css">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM