Template

#live demo #source code

In fact you can use any template-loader that webpack support, cause res.render just simply insert the string

You can get the complete webpack config of the example blow from here

may be you should install pug-loader or handlebars-loader first

1.using pug #demo

I think there is a bug with pug-loader in webpack3(2017-10-22), so you may better to install "pug-loader": "~2.2.1", and config the loader with ? before pug-loader fix this bug

webpack config

 module: {
    rules: [
      {
        test: /\.pug$/,
        loader: 'pug-loader?'
      }
  }
import pugTemplate from './autumn.pug'

export default function autumn (req, res, next) {
  var { params, query, body } = req
  res.render(pugTemplate(req))
}

2.using handlebars #demo

webpack config

 module: {
    rules: [
      {
        test: /\.hbs$/,
        use: {
          loader: 'handlebars-loader'
        }
      }
  }
import hbsTemplate from './autumn.hbs'

export default function autumn (req, res, next) {
  var { params, query, body } = req

  // Of course, you can use handlebars json helpers instead of using JSON.stringify
  res.render(hbsTemplate({
    paramsStr: JSON.stringify(params),
    queryStr: JSON.stringify(query),
    bodyStr: JSON.stringify(body),
    body: body
  }))
}
Last Updated: 8/10/2018, 1:49:32 AM