[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 events, and show how they can be used to manipulate the request or response in-flight.
const Hapi = require( 'hapi' )
const Boom = require( 'boom' )
const server = new Hapi.Server()
server.connection( { port: 8000 } ) server.ext( 'onRequest', ( request, reply ) => {
request.setUrl( '/' )
request.setMethod( 'GET' )
// need to call continue, the same as Express's next()
reply.continue()
} ) /**
* After the request has gone through the router, it needs to be authenticated. OnPreAuth runs before authentication, and onPostAuth runs after.
*/
server.ext( 'onRequest', ( request, reply ) => {
console.log( 'onRequest' )
reply.continue()
} ) /**
* After the request has gone through the router, it needs to be authenticated. OnPreAuth runs before authentication, and onPostAuth runs after.
*/
server.ext( 'onPreAuth', ( request, reply ) => {
console.log( 'onPreAuth' )
reply.continue()
} ) server.ext( 'onPostAuth', ( request, reply ) => {
console.log( 'onPostAuth' )
reply.continue()
} ) /**
* Validation is handled next. OnPreHandler runs, then, the route itself.
*/
server.ext( 'onPreHandler', ( request, reply ) => {
console.log( 'onPreHandler' )
reply.continue()
} ) /**
* OnPostHandler runs after the handler.
*/
server.ext( 'onPostHandler', ( request, reply ) => {
console.log( 'onPostHandler' )
reply.continue()
} ) /**
* Then, the response payload is validated on pre-response runs, and the response is sent to the client.
*/
server.ext( 'onPreResponse', ( request, reply ) => {
console.log( 'onPreResponse' )
reply.continue()
} ) server.route( {
method: 'GET',
path: '/',
handler: function ( request, reply ) {
console.log( 'handler' )
reply( 'hello world' )
}
} ) server.start( () => {
} )
What are these extensions good for? Each extension can be used to manipulate the request or response at any point in the lifecycle. For example, in onRequest, I can force all requests to be interpreted as GETs to the route path.
To do so, I'll use request.seturl, pass in the string/, and pass the string GET to request.setmethod. Now, when I make a post request to /foo, it still hits my defined route.
[Hapi.js] Extending the request with lifecycle events的更多相关文章
- [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 y ...
- [Hapi.js] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- [Hapi.js] Logging with good and good-console
hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [Hapi.js] Replying to Requests
hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...
- [Hapi.js] Route parameters
Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path param ...
随机推荐
- (转载)Linux网络配置和setup工具包安装
查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...
- AngularJS初步
AngularJS特点 遵循AMD规范 不需要操作节点 对于jquery,一般是利用现有完整的DOM,然后在这戏Dom的基础上进行二次调教了:而对于AngularJS等框架则是根据数据模型以及其对应用 ...
- 可用与禁用 E:enabled { sRules }
<!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...
- 如何把一个表中的部分字段值插入到另一个表中去 这sql吊
Insert into JHAC_TB_CODE(CID,CODE,ADD_TIME,USERID,PRO_CODE,USERNAME) select f_code.FID,f_code.Fcod ...
- BULK INSERT如何将大量数据高效地导入SQL Server
转载自:http://database.51cto.com/art/201108/282631.htm BULK INSERT如何将大量数据高效地导入SQL Server 本文我们详细介绍了BULK ...
- cxf的使用及安全校验-02创建简单的客户端接口
上一篇文章中,我们已经讲了如果简单的创建一个webservice接口 http://www.cnblogs.com/snowstar123/p/3395568.html 现在我们创建一个简单客户端接口 ...
- XML配置silverlight ,wcf 解析xml
XML 代码: <?xml version="1.0" encoding="utf-8" ?><ChartSet xmlns:xsi=&qu ...
- silverlight+wcf 项目 silverlight获得web程序的参数
silverlight 可以通过属性InitParams 获得参数,如果参数是动态的需要web程序传递的,具体操作如下: web程序后台:AppID,USERID需要的参数 this.frmRepor ...
- 修改UITextfield的Placeholder字体的颜色
- (void)viewDidLoad { [super viewDidLoad]; self.title=@"修改UITextField的placeholder字体颜色"; UI ...
- 线段树练习 codevs 1080
/* codevs 1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 一行N个方格,开 ...