h5bp

2019-10-12

h5bp是什么

什么是h5bp,HTML5 boilerplate (H5BP) inspired server config for node.js

h5bp使用教程帮助文档

h5bpbuild status">h5bpBuild Status


<g-emoji class="g-emoji" alias="warning" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/26a0.png">⚠️</g-emoji> This module is depecrated (Express 3.x) and is being rewritten.


HTML5 boilerplate(H5BP) server config for node.js.

h5bpfor node.js follows the guidelines of the Apacheversion:

  • secures backup and hidden files.
  • optionally redirects www.yoursite.tldto yoursite.tldor vice versa.
  • offers a simple cache busting mechanism.
  • normalize content types.
  • optionally enables CORS.
  • sets correct cache expires depending of the type of resource.
  • and some others...

It also focuses on offering additional features such as on-the-fly script concatenation using CommonJSor AMD.

Installation

npm install --save h5bp

Quick Start

Create a simple http server

var h5bp = require('h5bp');

var app = h5bp.createServer({ root: __dirname + '/public' });
app.listen(3000);

appis an instance of an expressapplication. You can add additional middlewares or routes if you like.

Use it as a connect / express middleware

var express = require('express'),
    h5bp = require('h5bp');

var app = express();
app.use(h5bp({ root: __dirname + '/public' }));

// in order to serve files, you should add the two following middlewares
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.listen(3000);

Concatenate scripts on-the-fly

If you want to split your application source files but only serve one file, you can use the on-the-fly concatenation. If you are familiar with node.js, you can use the CommonJSstyle. You can also use the AMDstyle.

app.use(h5bp({
    root: __dirname + '/public',
    scripts: {
        files: ['app.js'],
        processor: 'commonjs'   // can also be "amd"
    }
}));

At the first request hit to /app.js, the server will compile, cache and serve the file. Any subsequent request will serve the cached file without any performance impact.

So, this feature is meant to be used with the cache busting mechanismin order to ensure the client always has the latest resource version. If you restart your server, the cache will be flushed.

Note that the next release will provide a development modewhere the server will simply disable its cache and always serve the latest version of the file.

Options

There are several options you can pass to the middleware.

app.use(h5bp(options));

root

Tells the filesystem path to the root directory of static resources. This options is mandatory if you serve static files.

www

Forces wwwif true, forces non-wwwif false, does nothing if not defined. By default, this is disabled.

cors

Enables CORSfor everything. By default this is disabled.

dotfiles

Enables access to dotfiles. By default this is disabled.

scripts

Tells which scripts to concatenate.

This is an object with the following properties:

files

This is an array of files to concatenate. Their path is relative to the rootoption. Their URL will be absolute.

For example, if you set filesto ['scripts/app.js']and rootto /home/h5bp/app/:

  • The path will be: /home/h5bp/app/scripts/app.js.
  • The served URL will be: yoursite.tld/scripts/app.js.

processor

Tells which processor to use for scripts concatenation.

For now, it can be one of the following values:

  • commonjs: will concatenate files using the CommonJSmethod (require/exports).
  • amd: will concatenate files using the AMDmethod (require/define).

Additional options

The h5bp.createServerfunction takes the same options, plus additional ones.

The callbackis optional. It is a custom middleware that you can register directly if you want to.

h5bp.createServer(options, [callback]);

server

Tells which type of server you want to use.

It can be one of the following values:

  • express: uses express, this is the default value.
  • connect: uses connect.

logger

Tells if you want to log server requests or not. This can also be an object containing logger options.

compress

Tells if you want to serve gzippedcontent or not. By default this is true.

If you are using h5bpas a middleware, we strongly encourage you to use the compressmiddleware provided by express/ connect.

License

MIT License


上一篇:gulp-uglifyjs
下一篇:剖析setTimeout和click点击事件的触发顺序
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章
暂无相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部