[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‘)
            }
        }
    })
});
时间: 2024-11-03 22:04:45

[Hapi.js] Serving static files的相关文章

[Node.js] Serve Static Files with Express

In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Express. We will go over writing initial boilerplate for a simple Express app, and using Express's built-in middleware to serve these assets through a we

Adding Cache-Control headers to Static Files in ASP.NET Core

Thanks to the ASP.NET Core middleware pipeline, it is relatively simple to add additional HTTP headers to your application by using custom middleware. One common use case for this is to add caching headers. Allowing clients and CDNs to cache your con

前端技巧:禁止浏览器static files缓存篇(转)

前端技巧:禁止浏览器static files缓存篇 由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. 本文记录博主的经验. Meta法 目前在chrome调试还没有遇到问题,好用!此方法假设浏览器是个好人!很听我们的话! <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Cache-Control" content="

[Hapi.js] View engines

View engines, or template engines, allow you to maintain a clean separation between your presentation layer and the rest of your application. This post will demonstrate how to use the vision plugin with hapi to enable template support. index.js serve

[Hapi.js] Route parameters

Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path parameters in hapi's router. We'll also touch on how the router uses specificity to order routes internally. Router Param: http://localhost:8000/ztw // Wil

[Hapi.js] Request Validation with Joi

hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi's simple, 'use strict' const Hapi = require('hapi') const Joi = require('joi') const server

[Hapi.js] Managing State with Cookies

hapi has built-in support for parsing cookies from a request headers, and writing cookies to a response, making state management easy and straight-forward. It even has built in support for cookie encryption and auto detects when a cookie contains JSO

[Hapi.js] Friendly error pages with extension events

hapi automatically responds with JSON for any error passed to a route's reply()method. But what if your application needs errors rendered in HTML? This lesson shows how to implement friendly HTML error messages using hapi extension events. const Hapi

[Hapi.js] Extending the request with lifecycle events

Instead of using middlware, hapi provides a number of points during the lifecycle of a request that you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension even