本文整理汇总了TypeScript中hapi.Server.state方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.state方法的具体用法?TypeScript Server.state怎么用?TypeScript Server.state使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hapi.Server
的用法示例。
在下文中一共展示了Server.state方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
// From https://hapijs.com/api/16.1.1#serverstatename-options
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
// Set cookie definition
server.state('session', {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: true,
path: '/',
encoding: 'base64json'
});
// Set state in route handler
const handler: Hapi.RouteHandler = function (request, reply) {
let session = request.state.session;
if (!session) {
session = { user: 'joe' };
}
session.last = Date.now();
return reply('Success').state('session', session);
};
// Example 2
示例2: function
// from https://hapijs.com/tutorials/cookies?lang=en_US
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
server.state('data', {
ttl: null,
isSecure: true,
isHttpOnly: true,
encoding: 'base64json',
clearInvalid: false, // remove invalid cookies
strictHeader: true // don't allow violations of RFC 6265
});
server.route({
method: 'GET',
path: '/say-hello',
config: {
state: {
parse: true, // parse and store in request.state
failAction: 'error' // may also be 'ignore' or 'log'
}
},
handler: function(request, reply) {
// TODO test this
reply('Hello').state('data', { firstVisit: false });
}
})
示例3: handler
// from https://hapijs.com/tutorials/cookies?lang=en_US
import { Request, ResponseToolkit, Server, ServerOptions, ServerRoute, ServerStateCookieOptions } from "hapi";
const options: ServerOptions = {
port: 8000,
};
const serverRoute: ServerRoute = {
path: '/say-hello',
method: 'GET',
handler(request, h) {
return h.response('Hello').state('data', { firstVisit: false });
}
};
const server = new Server(options);
server.route(serverRoute);
const stateOption: ServerStateCookieOptions = {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: false,
isHttpOnly: false,
encoding: 'base64json',
};
server.state('data', stateOption);
server.start();
console.log('Server started at: ' + server.info.uri);