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


TypeScript Application.engine方法代码示例

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


在下文中一共展示了Application.engine方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: async

export default async (app: Application) => {
  if (config.database) {
    mongoose.connect(
      config.database.params.connection_string,
      {
        useNewUrlParser: true,
      },
    );
  }

  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());

  if (config.sessions) {
    app.use(clientSessions(config.sessions));
  }

  if (config.templates) {
    app.engine(config.templates.type, (await import(config.templates.type)).default.__express);
    app.set("view engine", config.templates.type);
  }

  if (config.pages) {
    config.pages.forEach((pageConfig: IPage) => {
      app.get(pageConfig.route, (_req: IRequest, res: Response) => {
        // req.headers
        if (pageConfig.template) {
          res.render(pageConfig.template, pageConfig.parameters);
        } else {
          res.status(200).json(pageConfig.parameters);
        }
      });
    });
  }

  if (config.static) {
    app.use(expressStatic(`__dirname${config.static}`));
  }

  if (config.collections) {
    config.collections.forEach((schema: ISchema) => {
      const modelSchema = model(schema);
      app.get(`/${schema.name}`, async (_req: IRequest, res: Response) =>
        res.status(200).json(await modelSchema.find()),
      );
      app.get(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
        try {
          res.status(200).json(await modelSchema.findOne({_id: req.params.id}));
        } catch (err) {
          res.status(404).json({error: "Not Found"});
        }
      });
      app.post(`/${schema.name}`, async (req: IRequest, res: Response) => {
        try {
          const newModel = new modelSchema(req.body);
          await newModel.save();
          res.status(201).json(newModel);
        } catch (err) {
          res.status(400).json({error: err.message});
        }
      });
      /*
      app.put(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
        //
      });
      app.patch(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
        //
      });
      app.delete(`/${schema.name}/:id`, (req: IRequest, res: Response) => {
        //
      });
      */
    });
  }

  /*if (config.auth) {
    config.auth.forEach((authConfig: IAuthConfig) => {
      //
    });
  }*/
};
开发者ID:dandart,项目名称:arsebeatfish,代码行数:81,代码来源:index.ts


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