如何在路由器文件中使用负载查看引擎?

问题描述

我正在尝试在我的route / profile.js文件中返回html文件。我试图使用哈巴狗和东西,但是,除了index.js之外,我找不到其他示例中声明app.set和app.engine的示例吗?

这是我的index.js

const port = config.port;
const app = express();

// Load View Engine

app.options('*',cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Routes
app.use('/gateway/userprofile',userProfile)

app.listen(port,() => console.log(`Server up and running on port ${port}`));

这是我的“ route / userProfile.js”文件的控制器

  static async sendVerificationEmail(req,res) {
    const userEmail = req.body.userEmail;
    console.log(userEmail);
    try {
      await axios.post(`http://123.42352.34234`,{
        type: 'UserProfileEmailVerification',userEmail,});
      console.log(
        colors.yellow.underline.bold(
          'Gateway -> BeUserProfile Sending Verification Email Succeed'
        )
      );
      res.sendStatus({ emailSent: 'successful' });
    } catch (err) {
      res.sendStatus(400);
    }
  }

我正在尝试返回HTML文件。有什么办法可以使用app.egine?

解决方法

在您的index.js上添加:

app.set('view engine','pug');
app.set('views',path.join(__dirname,'views'));

在这种情况下,“视图”是包含.pug文件的文件夹,您可以在任何文件上使用.pug

在此处检查完整的代码示例: https://github.com/jonasschmedtmann/complete-node-bootcamp/blob/master/4-natours/after-section-14/app.js