簡體   English   中英

Node.js加載CSS

[英]Node.js Loading css

我的文件夾:

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

服務器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">

當我在沒有服務器的情況下使用google chrome打開index.html時,CSS可以工作。當我使用node.js時,CSS無法加載。

express.static()直接在根目錄上提供其文件夾的內容

因此, public/不是URL的一部分。

節點:

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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM