Express Generator Express EJS template engine generator
A scaffolding, primarily used to quickly generate skeleton code
- Installation npm install -g express-generator
- Installation depends node install
- In the current directory template, ejs engine express --view = ejs ./
- Start running npm //package.json start with a default configuration
EJS template class analysis:
bin/www.js
Entry file, which is the main achievement of the three functions
- Open service, set the port
- Analytical and listening port, the Print window to the console
- Monitor abnormal
app.js
The main file services
- Exception Handling
- Logging
- 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(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
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 = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(500);
//参数是模板的名字
res.render('error');
});
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
Feature
- 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
app.js
var app = require('express')();
//设置视图
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//配置路由级中间件,在路由中测试
app.use('/test',testRouter);
module.exports = app;
test.js
var router = require("express").Router();
router.get('/include', (req, res) => {
//跳转到views目录下include.ejs
res.render('include', { user: [{ "name": "aaa" }, { "name": "bbb" }] });
})
module.exports = router;
include.ejs
<ul>
<% user.forEach((item)=>{ %>
<%- include('show',{user:item}) %>
<% })%>
</ul>