简体   繁体   English

无法在Node.js应用中提供样式表

[英]Unable to serve stylesheets in Node.js app

I am learning Node.js and Express (version 4.11). 我正在学习Node.js和Express(版本4.11)。 Currently, I am trying to create a basic web app that uses a stylesheet. 当前,我正在尝试创建一个使用样式表的基本Web应用程序。 In an attempt to do this, I have the following HTML: 为了做到这一点,我有以下HTML:

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="Description">
    <meta charset="UTF-8">

    <link rel="stylesheet" type="text/css" href="/public/fonts/icomoon.css" />
    <link rel="stylesheet" type="text/css" href="/public/css/style.css" />
</head>
<body>
  <h1 class="title">Test</h1>
</body>
</html>

When this HTML is loaded, I get 404 errors for my icomoon.css file and my style.css file. 加载此HTML时,我的icomoon.css文件和style.css文件出现404错误。 I'm confused, because I've followed the other similar post on SO. 我很困惑,因为我关注了SO上的其他类似文章。 My server.js file looks like the following: 我的server.js文件如下所示:

var express = require('express');                   // routing-engne
var expressHbs = require('express-handlebars');     // view-engine

// Setup the app to use Handlebars as the view engine
var app = express();
app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'layout.hbs'}));
app.set('view engine', 'hbs');

// Add support for static files (css, fonts, images, etc.)
app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
    var viewModel = {};
    res.render('/index', viewModel);
});

// Start the app on port 3000
app.listen(3000);
console.log('Ready!');

I thought the line written as app.use(express.static ... would serve up my static files. However, it is not. How do I serve up my .css files? 我认为写为app.use(express.static ...可以提供我的静态文件。但是,事实并非如此。如何提供我的.css文件?

Thank you! 谢谢!

用这个:

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

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

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