【问题标题】:How to set path for single page application in Hapi js如何在 Hapi js 中为单页应用程序设置路径
【发布时间】:2018-06-28 15:43:14
【问题描述】:

我试过这个https://github.com/hapijs/hapi/issues/800。这对我不起作用。

const start = async () => {

    await server.register(require('inert'));

    server.route({
        method: 'GET',
        path: '/samplespa/{file*}',
        handler: function (request, h) {
            directory :{
                path : './samplespa/'
                listing: true
            }
        }
    });

    server.ext('onPostHandler', (request, reply) => {
        console.log('WORD');
        const response = request.response;
        if (response.isBoom && (response.output.statusCode === 500)  ) {
        return reply.file('./samplemap.html');
        }
        return reply.continue;
    });



    await server.start();

    console.log('Server running at:', server.info.uri);
};

start();

我想为目录 samplespa 提供服务,并在其中渲染文件“index.html”,并注意 index.html 是用 Angular 1.X 编写的,并且依赖于目录中的文件..

同样用于路径:http://localhost:8000/samplespa/index.html

我收到以下回复

{
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
}

我在 vs 代码中收到以下错误消息:

Debug: internal, implementation, error
Error: handler method did not return a value, a promise, or throw an error
at module.exports.internals.Manager.execute (/Users/pavithran/projects/toilet-tracker/node_modules/hapi/lib/toolkit.js:52:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

如何做到这一点???在过去的 2 天里,我已经尝试了所有方法,但无法弄清楚..

【问题讨论】:

    标签: javascript server routes hapijs


    【解决方案1】:

    对于单个文件,您可以使用:

        server.route({
            method: 'GET',
            path: '/samplespa/index.html',
            handler: function (request, h) {
                return h.file('./samplespa/index.html');
            }
        });   
    

    对于多个文件,如下。请注意,处理程序现在不是函数而是对象。

        server.route({
            method: 'GET',
            path: '/samplespa/{file*}',
            handler: {
              directory: {
                path: 'samplespa'
              }
            },
        });   
    

    【讨论】:

    • 我其实知道这件事。但这只会呈现基本索引页面,而不是链接到索引页面的其他页面
    • 已编辑回复,请看一下。
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多