本文整理汇总了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) => {
//
});
}*/
};