简体   繁体   English

如何让玉加载css文件? (它不起作用)

[英]How to make jade loading the css file? (It doesn't work)

Here is my directory这是我的目录

enter image description here在此处输入图像描述

newTab.jade code newTab.jade 代码

doctype html
html
    head
        title New Tab
        link(rel = 'stylesheet', type = 'text/css', href = '/public/index.css')
    body
        p hello world

index.css code index.css代码

p{
    background-color: black;
    color: white;
}

app.js code app.js 代码

const express = require('express');
const app = express();
const port = 3000;
app.locals.pretty = true;
app.listen(port, function(){
    console.log("Server Connected port: "+port);
});

app.set('view engine', 'jade');
app.set('views', './views');

app.get('/', function(req, res){
    res.render('newTab');
});

jade file can't loading css file.玉文件无法加载 css 文件。 I tried href = "/public/index.css" but it doesn't work too.我试过 href = "/public/index.css" 但它也不起作用。

Append a middleware to Express to serve static files too. Append 是一个中间件,用于表达 static 文件。

In your app.js file:在您的 app.js 文件中:

// require path module to join your public folder with __dirname
const path = require('path');

//...

app.get('/', function(req, res){
    res.render('newTab');
});

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

There are more things express.static can do.快递的东西比较多express.static可以做。 Eg setting the maxAge for caching purpose:例如,为缓存目的设置maxAge

// 31557600000ms = 1 year
app.use('/', express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));

suggestion建议

try尝试

doctype html
html
    head
        title New Tab
         include index.css
    body
        p hello world

on your newTab.jade file在你的 newTab.jade 文件上

and include index.css on views directory并在视图目录中包含 index.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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