[Hapi.js] Serving static files

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Hapi.js] Serving static files相关的知识,希望对你有一定的参考价值。

hapi does not support serving static files out of the box. Instead it relies on a module called Inert. This lesson will cover serving static files using Inert‘s custom handlers.

 

‘use strict‘
var Hapi = require( ‘hapi‘ );
var Boom = require(‘boom‘);
var Path = require(‘path‘);

/**
 * set up server connection
 * @type {"hapi".Server}
 */
var server = new Hapi.Server();
server.connection( {
    host: ‘localhost‘,
    port: 8000
} );


server.register( require(‘inert‘), function(){

    // server an file
    server.route({
        method: ‘GET‘,
        path: ‘/2.png‘,
        handler: {
            file: Path.join(__dirname, ‘public/images/2.png‘)
        }
    });

    // server an directory
    server.route({
        method: ‘GET‘,
        path: ‘/images/{param*}‘,
        handler: {
            directory: {
                path: Path.join(__dirname, ‘public/images‘)
            }
        }
    })
});

 

以上是关于[Hapi.js] Serving static files的主要内容,如果未能解决你的问题,请参考以下文章