问题描述
我正在尝试在 NestJS
中使用 pug
布局,但是当从绝对路径扩展布局时,pug
需要 basedir
要设置的选项.
I'm trying to use pug
layouts in NestJS
, however when extending a layout from an absolute path, pug
requires the basedir
option to be set.
在 ExpressJS 中你会使用 app.locals.basedir = ...
,那么 NestJS 中的等价物是什么?
In ExpressJS you would use app.locals.basedir = ...
, what would be the equivalent in NestJS?
const server = await NestFactory.create<NestExpressApplication>(AppModule);
server.setViewEngine('pug');
server.setBaseViewsDir(join(__dirname, 'templates', 'views'));
await server.listen(config.server.port);
在视图中使用 extends/layouts/index
会抛出以下内容;basedir"选项需要使用包含和绝对"路径扩展
.
Using extends /layouts/index
in a view would throw the following; the "basedir" option is required to use includes and extends with "absolute" paths
.
我不打算使用相对路径,因为这很快就会变得非常混乱.例如.扩展../../../layouts/index
I'm not looking to use relative paths, since this quickly becomes very messy. E.g. extends ../../../layouts/index
推荐答案
翻阅文档后,NestJS 在幕后使用了一个 express,并通过 getHttpAdapter().getInstance()
.
After looking around through the documentation, NestJS uses an express under the hood, and gives you access to the underlying instance with getHttpAdapter().getInstance()
.
记住这一点,我们可以如下设置basedir
;
Keeping that in mind, we can set the basedir
as follows;
const express = server.getHttpAdapter().getInstance();
express.locals.basedir = join(__dirname, 'templates');
这篇关于设置“basedir"NestJS 中的 Pug 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!