当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Server.views方法代码示例

本文整理汇总了TypeScript中gearworks.Server.views方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.views方法的具体用法?TypeScript Server.views怎么用?TypeScript Server.views使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gearworks.Server的用法示例。


在下文中一共展示了Server.views方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
    
    //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}`);
    })
}
开发者ID:nozzlegear,项目名称:deliver-on,代码行数:87,代码来源:server.ts


注:本文中的gearworks.Server.views方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。