Node EJS Sample #120
Replies: 3 comments
-
Hello @preshndams, this plugin is for the Webpack, not for Express framework. |
Beta Was this translation helpful? Give feedback.
-
Is there a way I can add data asynchronously from the server when building the bundle? I have an express backend that fetches the data and passes it as props to the ejs templates. |
Beta Was this translation helpful? Give feedback.
-
You can use the EJS templates directly in Express to generate HTML dynamically. See the official doc here. The Webpack plugin generates static HTML files from EJS templates. There is no way to generate dynamic HTML with Webpack on backend. Also, following does not work: Browser request > read data from DB on backend > generate HTML via Webpack > output to Browser. But you can generate static HTML with external variables: const path = require('path');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
output: {
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new HtmlBundlerPlugin({
entry: [
{
import: 'src/views/home.html',
filename: 'index.html', // save generated HTML into dist/index.html
data: require('path/to/getCustomData.js'), // <= here you can pass external data into EJS template
}
],
preprocessor: 'ejs',
}),
],
};
const data = { user: { name: 'Max', age: '20' } }; // <= here you can define data for the template
module.exports = data; |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm interested in integrating Node Express with EJS. I'd like to use data from a server.js file. Can you provide a sample that shows how to do this?
Beta Was this translation helpful? Give feedback.
All reactions