The underlying API of Node.js will make people feel very cumbersome, and it is necessary to write multiple lines of code to listen to a GET request. Express is an open Web framework based on Node.js, which is encapsulated and allows developers to focus only on business logic development. It is also based on middleware development mode, with strong expansibility and core concept of Express

  1. routing
  2. The middleware
  3. A template engine

1. The routing

1. Four types of routes are roughly supported

  1. String type

  2. String template type

  3. Regular expression type

  4. The parameter types

    Var express = require(‘express’) var app = express() app.get(‘/user’, callback) /user/woman app.get(‘/user/*man’, callback) // Regular route app.get(/person$/, callback) // route parameter app.get(‘/ API /:uid/:age’)

2. Split routes

Before Route splitting

app.get('/user/list', callback)
app.get('/user/detail', callback)
Copy the code

This leads to the problem of maintenance with /user and poor maintainability

Express.router () can be used to split routes

var express = require('express')
var app = express()
var user = express.Router()

user.get('/list', callback)
user.get('/detail', callback)
app.use('/user', user)
Copy the code

The basic split method is already out, but for larger applications, we also have split file management, file directory

|--routes
    |--index.js
    |--user.js
|--app.js
Copy the code

The user.js file writes the user API content

var express = require('express')
var route = express.Router()
route.get('/list', (req, res) => {
    res.end('list')
})
route.get('/detail', ( req, res ) => {
    res.end('detail')
})
module.exports = route
Copy the code

Index.js as the entry file to the router

var express = require('express')
var routes = express.Router()
var user = require('./user.js')

routes.use('/user', user)
module.exports = routes
Copy the code

App.js application entry, adding a top-level routing API

var express = require('express')
var app = express()

var routers = require('./routes/index.js')
app.use('/api', routers)
app.listen(300)
Copy the code

The access address is, and the route directory is set up

http://localhost:3000/api/user/list
http://localhost:3000/api/user/detail
Copy the code

2. The middleware

Learning Express means everything is middleware, such as request parameter parsing, cookie parsing, etc

1. We write a simple middleware

  • Three parameters, reQ client request instance, RES server return instance

  • Next callback method, when next() executes, proceed to the next middleware, otherwise pedding

    var middleFunc = function( req, res, Next) {console.log(‘ I am middle ware’) next()} app.use(middleFunc) // Console output I am middle ware

2. Common middleware

  • bode-parser
  • compression
  • serve-static
  • session
  • cookie-parser
  • morgan

3. Template engines

If you wanted to render a page, what would happen if you didn’t have a template engine, and you had to write out all the template strings, and then insert them into the page and render, complicated and error-prone

App.get ('/', function(req, res) {var HTML = '< HTML >' res.send(HTML)})Copy the code

But if there is a template engine that efficiency is small horse for taxi, no longer worry about template, take EJS template engine as an example

/ / create a new folder view | - view | -- index. EjsCopy the code

App.js configures the template engine

app.set('views', __dirname + '/view') app.set('view engine', 'ejs') app.use('/', function( req, Res) {res.render('index', {title: 'home '})})Copy the code

Ejs template, which is basically HTML syntax

<! DOCTYPE HTML >< HTML >< head></head> <body> <div> Template engine EJS </div> <%= title %> </body> </ HTML >Copy the code
  • App. set is Express set parameter tool views, a directory for storing template files
  • View Engine View template engine, in this case EJS
  • Res.render (‘index’, obj) index is the template index.ejs file, obj is the passed argument
  • <%= title %> after the argument is passed in

More ejS documentation can be found at ejs.bootcss.com/#install

Node writes to the server, vue/React writes to the client, or vue/React renders to the server.