0

APIルートとReactアプリケーションを提供するhapijsサーバーがあります。inert と、ディレクトリ パラメーターを指定したハンドラーを使用して、react アプリケーションを提供します。ただし、パス {param*} で使用するパラメーターは自動的に「index.html」を提供するため、hapijs はパラメーターがない場合にのみ反応ページを提供します。これは、hapi がディレクトリ内でパラメーターを検索し、エラー 404 で応答するため、理にかなっています。

今、私がやりたいことは、パラメーター (またはルート) に関係なく反応ページを提供し、反応に 404 エラーを処理させることです。合法的な解決策を見つけることができないようです。ディレクトリ ハンドラーを使用すると 404 が許可されず、関数ハンドラーを使用してページを提供すると、css または js スクリプトなしでページが提供されます。

次のチュートリアルを使用し、他のチュートリアルを読みましたが、役に立ちませんでした。 https://medium.com/@notrab/using-create-react-app-with-hapi-js-8f4ef3dcd311

//manifest.js
module.exports = {
    server: {
        port: process.env.PORT,
        router: { stripTrailingSlash: true },
        routes: {
            files: {relativeTo: path.join(__dirname, '../client/build')},
            cors: {origin: ["*"],}
        }
    },
    register: {
        plugins: [
            { plugin: require('inert') },
            {
                plugin: 'vision',
                options: {
                    engines: { html: require('handlebars') },
                    path: path.join(__dirname, '../client/build')
                }
            },
            {
                plugin: require('./controllers/home'),
            },
        ],
    }
// controller/home
module.exports.register = function(server, options, next) {
server.route({
    method: 'GET',
    path: '/{param*}',
    handler: {
    directory: {
    path: path.join(__dirname, "../../../client/build/"),
    listing: false,
    index: ['index.html']
}

// the following handler serves the page but not the css or js
    //handler: (request, h) => {
    //    return h.file("index.html", {confine: false});
    //}
4

1 に答える 1