简体   繁体   中英

Serving static JavaScript file in Hapi.js not working

I have created a static route like this:

"use strict";

var StaticPlugin = {
    register: function (server, options, next) {
        server.route([
            {
                method: 'GET',
                path: '/public/{path*}',
                config: {
                    auth: false,
                    description: 'Static assets',
                    handler: {
                        directory: {
                            path: '../public',
                            index: false,
                            listing: false
                        }
                    }
                }
            }
        ]);

        return next();
    }
};

StaticPlugin.register.attributes = {
    name: 'StaticPlugin',
    version: '1.0.0'
};

module.exports = StaticPlugin;

I register the plugin, but when I attempt to try to load a static JS file (going to be used to serve static js libraries), I get a 404 error.

The url I am trying to hit is http://localhost:3000/public/javascripts/mylib.js

In your configuration the path is relative to the directory where the process is running. If this plugin is in a subdirectory and you start the server in the main directory, then the path will be incorrect. Either use a path that is relative to the project root, or use Path.join() in conjunction with __dirname , eg:

var Path = require('path');
Path.join(__dirname, '../public')

您必须确保使用插件“ Inert”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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