Add more test files.

This commit is contained in:
rafaelsouzaf
2017-12-09 16:41:54 -03:00
parent 6fd4625f82
commit 2cbd2d48e8
20 changed files with 164 additions and 31 deletions

View File

@@ -8,26 +8,36 @@
/// <reference types="node" />
/** Getting Started */
export * from './getting-started/01-creating-a-server';
export * from './getting-started/02-adding-routes';
export * from './path/catch-all';
export * from './path/parameters';
export * from './reply/continue';
export * from './reply/redirect';
export * from './reply/reply';
export * from './reply/state_cookie';
/** REQUEST */
export * from './request/catch-all';
export * from './request/event-types';
export * from './request/get-log';
export * from './request/parameters';
export * from './request/query';
/** RESPONSE */
export * from './response/continue';
export * from './response/error';
export * from './response/redirect';
export * from './response/reply';
export * from './response/state_cookie';
/** ROUTE */
export * from './route/adding-routes';
export * from './route/config';
export * from './route/handler';
/** SERVER */
export * from './server/a-simple-server';
export * from './server/app';
export * from './server/info';
export * from './server/select';
export * from './server/server-events-once';
export * from './server/settings';
export * from './server/start';
export * from './server/stop';
export * from './server/table';
export * from './server/version';

View File

@@ -22,7 +22,6 @@ server.start();
console.log('Server started at: ' + server.info.uri);
server.events.on('request', (request: Request, event: any, tags: any) => {
console.log(event);
console.log(tags);
if (tags.error) {
console.log(event);

View File

@@ -17,7 +17,7 @@ const serverRoutes: ServerRoute[] = [
return response;
}
},
// Chained notation
// chained notation
{
path: '/test2',
method: 'GET',
@@ -34,4 +34,4 @@ const server = new Server(options);
server.route(serverRoutes);
server.start();
console.log('Server started at: ' + server.info.uri);
console.log('Server started at: ' + server.info.uri);

View File

@@ -6,10 +6,9 @@ const options: ServerOptions = {
};
const serverRoute: ServerRoute = {
path: '/test',
path: '/',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
console.log(request);
return 'ok: ' + request.path;
}
};
@@ -19,7 +18,6 @@ const serverRoutes: ServerRoute[] = [
path: '/test1',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
console.log(request);
return 'ok: ' + request.path;
}
},
@@ -27,7 +25,6 @@ const serverRoutes: ServerRoute[] = [
path: '/test2',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
console.log(request);
return 'ok: ' + request.path;
}
},

View File

@@ -0,0 +1,54 @@
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute, RouteOptions} from "hapi";
const options: ServerOptions = {
port: 8000,
};
// different methods
var routeConfig: ServerRoute = {
path: '/signin',
method: 'PUT',
vhost: 'site.coms',
};
var routeConfig: ServerRoute = {
path: '/signin',
method: '*'
};
var routeConfig: ServerRoute = {
path: '/signin',
method: ['OPTIONS', '*']
};
// different handlers
var routeConfig: ServerRoute = {
path: '/signin',
method: 'PUT',
handler: function (request: Request, h: ResponseToolkit) {
return 'ok';
}
};
var routeConfig: ServerRoute = {
path: '/signin',
method: 'PUT',
handler: function (request: Request, h: ResponseToolkit) {
return 'ok';
}
};
const server = new Server(options);
server.route(routeConfig);
// Handler in config
const user: RouteOptions = {
cache: { expiresIn: 5000 },
handler: function (request: Request, h: ResponseToolkit) {
return { name: 'John' };
}
};
server.route({method: 'GET', path: '/user', options: user });
//server.route({ method: 'GET', path: '/public/{path*}', options: { cache: { privacy: 'public', expiresIn: 24 * 60 * 60 * 1000 } }, handler: { directory: { path: __dirname, listing: false, index: false } } });
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,9 @@
import {Lifecycle, Request, ResponseToolkit} from "hapi";
const handler: Lifecycle.Method = function(request: Request, h: ResponseToolkit) {
return 'success';
}
const strictHandler: Lifecycle.Method = function(request: Request, h: ResponseToolkit) {
return 123;
}

View File

@@ -19,4 +19,4 @@ const serverRoute: ServerRoute = {
server.route(serverRoute);
server.start();
console.log(`Server running at: ${server.info!.uri}`);
console.log('Server started at: ' + server.info.uri);

View File

@@ -1,15 +1,15 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-serverinfo
import {Server, ServerOptions} from "hapi";
import assert = require('assert');
import {Server} from "hapi";
import * as assert from "assert";
const options: ServerOptions = {
const server = new Server({
port: 8000,
};
const server = new Server(options);
});
server.start();
// check the correct port
if(server.info) assert(server.info.port === 8000);
assert(server.info !== null);
console.log(server.info);
if (server.info) assert(server.info.port === 8000);
server.start();
assert(server.info !== null);
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,17 @@
// From https://hapijs.com/api/16.1.1#serverselectlabels
import {Server} from "hapi";
/*
const server = new Hapi.Server();
server.connection({ port: 80, labels: ['a', 'b'] });
server.connection({ port: 8080, labels: ['a', 'c'] });
server.connection({ port: 8081, labels: ['b', 'c'] });
const a = server.select('a'); // 80, 8080
const ac = a.select('c'); // 8080
*/
// TODO How do this with v17?
const server = new Server({
port: 8000,
});

View File

@@ -1,10 +1,6 @@
// from https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncecriteria-listener
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
const options: ServerOptions = {
port: 8000,
};
const serverRoute: ServerRoute = {
path: '/',
method: 'GET',
@@ -13,7 +9,9 @@ const serverRoute: ServerRoute = {
}
};
const server = new Server(options);
const server = new Server({
port: 8000,
});
server.route(serverRoute);
server.event('test1');
server.event('test2');

View File

@@ -0,0 +1,12 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-serversettings
import {Server} from "hapi";
const server = new Server({
port: 8000,
app: {
key: 'value'
}
});
server.start();
console.log(server.settings.app); // { key: 'value' }

View File

@@ -0,0 +1,11 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-await-serverstart
import {Server} from "hapi";
const server = new Server({
port: 8000,
});
server.start();
server.events.on('start', () => {
console.log('Server started at: ' + server.info.uri);
});

View File

@@ -0,0 +1,17 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-await-serverstopoptions
import {Server} from "hapi";
const server = new Server({
port: 8000,
});
server.start();
server.events.on('start', () => {
console.log('Server started at: ' + server.info.uri);
});
server.events.on('stop', () => {
console.log('Server stoped.');
});
setTimeout(function() {
server.stop({ timeout: 10 * 1000 });
}, 5 * 1000);

View File

@@ -0,0 +1,9 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-serverversion
import {Server} from "hapi";
const server = new Server({
port: 8000,
});
server.start();
console.log(server.version); // 17.x.x