Hapi.js - 无法在SPA中提供静态文件

时间:2015-04-12 18:27:16

标签: node.js static-files hapijs

我刚刚尝试制作SPA,但我无法解析index.html依赖项(例如包括相关的css,js文件)。所有js和css文件都无法加载到浏览器中并在开发控制台中显示404错误。

/root
|
|__public
|      └── spa
|          ├── controllers
|          ├── css
|          ├── html
|          ├── js-self
|          ├── js-vendor
|          └── services
|_____________ index.html

路线声明

var assets = function(){
    return {
        method: 'GET',
        path: '/{param*}',
        handler: {
            directory :{
                path : 'public',
                index: false
            }
        }
    }
}

所有路线的顺序

  info: ================Registered routes=============
info: Route:    GET /employee
info: Route:    POST    /employee
info: Route:    PUT /employee
info: Route:    DELETE  /employee
info: Route:    POST    /signin
info: Route:    POST    /signup
info: Route:    POST    /signout
info: Route:    GET /{param*}
info: Route:    GET /
info: =============================================

Html文件

<!DOCTYPE html>
<!-- saved from url=(0058)http://getbootstrap.com/examples/starter-template/#contact -->
<html lang="en" ng-app="ibs">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Starter Template for Bootstrap</title>
    <script src="./public/spa/js-vendor/jquery-2.1.3.min.js"></script>
    <script src="./public/spa/js-vendor/angular-1.3.14.js"></script>
    <script src="./public/spa/js-vendor/angular-route.js"></script>
    <script src="./public/spa/app.js"></script>
    <link href="./public/spa/css/signin.css" rel="stylesheet">
    <link href="./public/spa/css/bootstrap.min.css" rel="stylesheet">
    <link href="./public/spa/css/starter-template.css" rel="stylesheet">

    <style type="text/css" media="screen">
        .appbkg {
          background: #303F9F
        }
    </style>
  </head>

  <body class="appbkg">
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="http://getbootstrap.com/examples/starter-template/#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="http://getbootstrap.com/examples/starter-template/#">Home</a></li>
            <li><a href="http://getbootstrap.com/examples/starter-template/#about">About</a></li>
            <li><a href="./Starter Template for Bootstrap_files/Starter Template for Bootstrap.html">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container" ng-view>
    </div><!-- /.container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="./public/spa/js-vendor/bootstrap.js"></script>
</body></html>

1 个答案:

答案 0 :(得分:8)

我使用了这个小编辑:

var assets = function(){
return {
    method: 'GET',
    path: '/{param*}',
    handler: {
        directory :{
            path : './',
            index: false
        }
    }
}
}

./路径实际上相对于您运行node app.js时的位置。

因此,请确保在运行它时位于根文件夹中。由于node ../app.js不起作用!!

由于这不是很干净,避免使用路径并坚持使用带有__dirname的path.join(__dirname, 'path/to/folder')作为参考。 这是一个更清洁的解决方案

var assets = function(){
return {
    method: 'GET',
    path: '/{param*}',
    handler: {
        directory :{
            path : path.join(__dirname, '../..'),
            index: false
        }
    }
}
}

注意from node's docs

__dirname是:

  

当前正在执行的脚本所在的目录的名称。