How Egg.js Supports Single Page Application
- Enable ejs in config/plugin.js // Other template engines also work
exports.ejs = {
enable: true,
package: 'egg-view-ejs',
};
- Configure static directory
//config/config.{env}.js
config.static = {
prefix: '/',
dir: path.join(appInfo.baseDir, 'app/view/')
}
- Template configuration
//config/config.{env}.js
config.view = {
defaultExt: '.html',
mapping: {
'.ejs': 'ejs',
'.html': 'ejs',
}
}
- Configure root directory route mapping
// app/router.js
module.exports = app => {
app.router.get('/', app.controller.home.index);
}
- Configure route corresponding controller
// app/controller/home.js
const { Controller } = require('egg');
class HomeController extends Controller {
async index() {
const { ctx } = this;
// render index.html
await ctx.render('index');
}
}
module.exports = HomeController;
After completing the above configuration, you can combine with single page application