本文整理匯總了TypeScript中gearworks.Server.start方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.start方法的具體用法?TypeScript Server.start怎麽用?TypeScript Server.start使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gearworks.Server
的用法示例。
在下文中一共展示了Server.start方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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}`);
})
}