Express Generator Express EJS template engine generator

Express Generator Express EJS template engine generator

A scaffolding, primarily used to quickly generate skeleton code

  1. Installation npm install -g express-generator
  2. Installation depends node install
  3. In the current directory template, ejs engine express --view = ejs ./
  4. Start running npm //package.json start with a default configuration

EJS template class analysis:


Entry file, which is the main achievement of the three functions

  1. Open service, set the port
  2. Analytical and listening port, the Print window to the console
  3. Monitor abnormal


The main file services

  1. Exception Handling
  2. Logging
  3. Route navigation
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404, 'file not found!'));

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error ='env') === 'development' ? err : {};
  // render the error page

module.exports = app;

EJS template engine

About EJS

A simple and efficient template language, and templates through the data, you can generate html tag text
a JavaScript library, and then you can run the client and server at the same time


  • Fast compilation and rendering
  • Simple template tags
  • Custom tag delimiter
  • Support text contains
  • Supports B, C terminal using
  • Template static cache
  • Support express view of the system

EJS member function

Render (path, data, [option]): path is the template path, data is passed template data, option is a configuration option

Commonly used tags

  • <%> Tag flow control, as commonly used in the cycle forEach
  • <% =%> Tag output, for extracting data is output to the html
  • <% -%> tag output may be used include
  • <% #%> Comment tags

EJS Case


var app = require('express')();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
module.exports = app;


var router = require("express").Router();

router.get('/include', (req, res) => {
    res.render('include', { user: [{ "name": "aaa" }, { "name": "bbb" }] });

module.exports = router;


        <% user.forEach((item)=>{ %>
        <%- include('show',{user:item}) %>
        <% })%>


Guess you like