本文整理汇总了TypeScript中gearworks.Server.ext方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.ext方法的具体用法?TypeScript Server.ext怎么用?TypeScript Server.ext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gearworks.Server
的用法示例。
在下文中一共展示了Server.ext方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: startServer
async function startServer()
{
//Validate environment variables
Object.getOwnPropertyNames(config).forEach((prop, index, propNames) =>
{
// Ensure config prop isn't optional
if (config.OptionalProps.indexOf(prop) === -1 && typeof config[prop] === "undefined")
{
throw new Error(`Configuration property ${prop} cannot be null or empty. Check modules/config.ts to find the correct environment variable key for ${prop}.`);
}
})
//Configure the server's app state
server.app = {
appName: config.AppName,
isLive: config.isLive,
rootDir: path.resolve(__dirname),
};
await registerPlugins();
//Configure authentication. Must be done before configuring routes
configureAuth(server);
//Filter all responses to check if they have an error. If so, render the error page.
server.ext("onPreResponse", (request, reply) =>
{
const resp = request.response;
if (resp.header)
{
resp.header("X-STAGES-API-VERSION", "1.0.0");
}
return reply.continue();
});
//Register all app routes.
RoutesToRegister.forEach((register) => register(server));
return server.start((error) =>
{
if (error)
{
throw error;
}
console.log(`${server.app.isLive ? "Live" : "Development"} server running at ${server.info.uri}`);
})
}
示例2: configureAuth
export function configureAuth(server: Server)
{
//Configure a preresponse handler that will add the user's auth information to all view contexts
server.ext("onPreResponse", (request: Request, reply) =>
{
if (request.response.variety === "view")
{
const context: DefaultContext = request.response.source.context || {};
context.user = {
isAuthenticated: request.auth.isAuthenticated,
userId: undefined,
username: undefined,
}
if (request.auth.isAuthenticated)
{
context.user.userId = request.auth.credentials.userId;
context.user.username = request.auth.credentials.username;
}
}
return reply.continue();
})
const fullScheme = "full";
const basicScheme = "basic";
const shopifyRequestScheme = "shopify-request";
const shopifyWebhookScheme = "shopify-webhook";
server.auth.scheme(fullScheme, (s, options) => ({
authenticate: async (request, reply) =>
{
function unauthorized()
{
const response = request.generateResponse().redirect(AuthRoutes.GetLogin);
// Response is ignored if error is passed in as first param
return reply(null, response);
}
let auth: {credentials: AuthCredentials; artifacts: AuthArtifacts};
try
{
auth = await getUserAuth(request);
}
catch (e)
{
if (e.status === 404)
{
return unauthorized();
}
return reply(boom.wrap(e));
}
if (!auth)
{
return unauthorized();
}
if (!auth.artifacts.shopToken)
{
const response = request.generateResponse().redirect(SetupRoutes.GetSetup);
return reply(null, response, auth);
}
if (!auth.artifacts.planId)
{
const response = request.generateResponse().redirect(SetupRoutes.GetPlans);
return reply(null, response, auth);
}
return reply.continue(auth);
}
}));
server.auth.scheme(basicScheme, (s, options) => ({
authenticate: async (request, reply) =>
{
function unauthorized()
{
const response = request.generateResponse().redirect(AuthRoutes.GetLogin);
// Response is ignored if error is passed in as first param
return reply(null, response);
}
let auth: {credentials: AuthCredentials; artifacts: AuthArtifacts};
try
{
auth = await getUserAuth(request);
}
catch (e)
//.........这里部分代码省略.........
示例3: startServer
async function startServer()
{
//Validate environment variables
Object.getOwnPropertyNames(config).forEach((prop, index, propNames) =>
{
// Ensure config prop isn't optional
if (config.OptionalProps.indexOf(prop) === -1 && typeof config[prop] === "undefined")
{
throw new Error(`Configuration property ${prop} cannot be null or empty. Check modules/config.ts to find the correct environment variable key for ${prop}.`);
}
})
//Configure the server's app state
server.app = {
appName: config.AppName,
isLive: config.isLive,
rootDir: path.resolve(__dirname),
};
await registerPlugins();
//Configure authentication. Must be done before configuring routes
configureAuth(server);
//Set the viewengine to use react
server.views({
engines: {
js: require("hapi-react-views")
},
compileOptions: {},
relativeTo: server.app.rootDir,
path: "views",
context: {
appName: server.app.appName,
isLive: server.app.isLive,
} as DefaultContext
});
//Filter all responses to check if they have an error. If so, render the error page.
server.ext("onPreResponse", (request, reply) =>
{
const resp = request.response;
if (resp.header)
{
resp.header("X-POWERED-BY", "Gearworks https://github.com/nozzlegear/gearworks");
}
if (request.route.path === ApiRoutes.AppConfig)
{
// API should return a plain JSON response, not the error view.
return reply.continue();
}
if (resp.isBoom || isError(resp))
{
const resp: Boom.BoomError = request.response as any;
const payload: { error: string, message: string, statusCode: number } = resp.output.payload;
const props: ErrorPageProps = {
errorType: payload.error,
message: payload.message,
statusCode: payload.statusCode,
title: payload.error,
};
console.log(`${payload.statusCode} ${payload.error} for ${request.url.pathname}. ${resp.message}.`, payload.statusCode >= 500 ? util.inspect(resp) : "");
return (reply.view("errors/error.js", props)).code(payload.statusCode);
}
return reply.continue();
});
//Register all app routes.
RoutesToRegister.forEach((register) => register(server));
return server.start((error) =>
{
if (error)
{
throw error;
}
console.log(`${server.app.isLive ? "Live" : "Development"} server running at ${server.info.uri}`);
})
}