Hapi.js视图未加载CSS文件

eak*_*akl 1 html javascript css handlebars.js hapijs

我在使用和将app.css文件链接到default.html视图时遇到问题。我使用Cloud9进行托管。hapijshandlebars.js

这是我的应用程序结构:

root/
   client/
      config/
         route.js
      css/
         app.css
      view/
         layout/
            default.html
         index.html
server.js
Run Code Online (Sandbox Code Playgroud)

我的 server.js

'use strict';

const Hapi      = require('hapi');
const Vision    = require('vision');
const Inert     = require('inert');
const Path      = require('path');
const Routes    = require('./client/config/route')

const server = new Hapi.Server();

server.connection({
    host: process.env.IP || '0.0.0.0',
    port: process.env.PORT || 3000
});

server.register([Vision, Inert], (err) => {
    if (err) {
        throw err;
    }

    server.views({
        engines: {
            html: require('handlebars')
        },
        relativeTo: Path.join(__dirname, 'client'),
        path: 'views',
        layoutPath: 'views/layouts',
        layout: 'default', // true
        partialsPath: 'views/partials'
    });

    server.route(Routes);
});

server.start(() => {
    console.log('Server started at: ' + server.info.uri);
});
Run Code Online (Sandbox Code Playgroud)

我的route.js

const Path  = require('path');

const routes = [
    {
        method: 'GET',
        path: '/{param*}',
        handler: {
            directory: {
                path: Path.join(__dirname, 'client'),
                listing: false,
                index: false
            }
        }
    },
    {
        method: 'GET',
        path: '/',
        handler: (req, rep) => {

            let data = { };                
            rep.view('index', data);
        }
    }
];

module.exports = routes;
Run Code Online (Sandbox Code Playgroud)

我的default.html

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8" />
        <title>Title</title>
        <link rel="stylesheet" href="../../css/app.css" />
    </head>
    <body>
        {{{content}}}
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的index.html

<p id="paragraph"><p>
Run Code Online (Sandbox Code Playgroud)

问题是我app.css没有加载。我按照这个建议这一个,但它仍然无法正常工作,我不知道接下来要检查什么。

小智 5

您可以编写一条单独的路线来服务您的资源文件

如果cssjs文件夹在resources文件夹中,则

server.route({
path: "/resources/{path*}",
method: "GET",
handler: {
    directory: {
        path: "./resources",
        listing: false,
        index: false
    }
}});
Run Code Online (Sandbox Code Playgroud)

此路由将为您提供静态内容,然后您可以将这些文件添加到html文件中。

<link rel="stylesheet" href="resources/css/main.css" />
Run Code Online (Sandbox Code Playgroud)

有关更多参考,请检查它 http://jaskokoyn.com/2014/07/28/views-node-js-tutorial-beginners/