OverviewNode middleware is the function of encapsulating http requests in the program. Node middleware is executed in the pipeline. Middleware sits on top of the client/server operating system and manages computer resources and network communications. Middleware serves the main logical business and can be divided into: application-level middleware, routing-level middleware, built-in middleware, third-party middleware, and error-level middleware. 1. Application-level middlewareEach middleware calls a function and needs to be used with other middleware or routing. server (function) intercepts all routes server.use('/reg', function); intercept specific routes const express = require('express'); var server = express(); server.listen(3000); //Middleware interception registration route/reg server.use('/reg',(req,res,next)=>{ console.log('Registration verification'); next(); }); server.use('/login',(req,res)=>{ console.log('Log recorded'); }) server.get('/reg',(req,res,next)=>{ res.send('Registration successful'); next(); }); server.get('/login',(req,res)=>{ res.send('Login successful'); }) 2. Built-in middlewareserver.use(express.static('目录')) Host static resource files in a certain directory. If the browser requests static resources, it will automatically search in this directory. 3. Third-party middleware(1) body-parser, parses post request data into objectsconst express = require('express'); //Introduce body-parser middleware const bodyParser=require('body-parser'); var server = express(); server.listen(3000); // Host static resources to public server.use(express.static('./public')); //Use body-parser middleware to parse the post request data into an object //Extended Whether to use the extended qs module to parse into an object //If false, do not use it, and use the querystring module instead server.use(bodyParser.urlencoded( {extended:false} )) server.post('/mylogin',(req,res)=>{ console.log(req.body); res.send('Login successful'); }) (2) mysql moduleNormal connection //Introduce the mysql module const mysql =require('mysql'); //1. Normal connection //1.1 Create a connection var connection = mysql.createConnection({ host:'127.0.0.1', port:'3306', user:'root', password:'1234', database:'tedu' }); //1.2 Execute connection connection.connect(); //Execute SQL statement connection.query('select * from emp',(err,result)=>{ if(err) throw err; console.log(result); }); //Close the connectionconnection.end(); Connection Pool const mysql = require('mysql'); //Use connection pool //Create connection pool object var pool = mysql.createPool( { host:'127.0.0.1', port:'3306', user:'root', password:'1234', database:'tedu', connectionLimit:20 } ); //Execute SQL statement pool.query('select * from emp where eid=2',(err,result)=>{ if(err) throw err; console.log(result); }) const mysql = require('mysql'); var pool = mysql.createPool({ 'host':'127.0.0.1', port:'3306', user:'root', password:'1234', database:'tedu', connectionLimit:20 }) //Use placeholders to insert data // pool.query("insert into emp values(?,?,?,?,?,?)",[null,'yt','1','1995-12-24','9000','20'],(err,result)=>{ // if(err) throw err; // console.log(result); // }); //Use object to insert data//var emp={ // eid:null, // ename:'html', // sex:1, // birthday:'1995-12-25', //salary:10000, //deptid:30 // } // pool.query('insert into emp set ?',[emp],(err,result)=>{ // if(err) throw err; // console.log(result); // }) //Use the placeholder to delete the data numbered 10 pool.query('delete from emp where eid=?',[10],(err,result)=>{ if(err) throw err; console.log(result); if(result.affectedRows>0){ console.log('Deleted successfully'); } else{ console.log('deletion failed'); } }) The above is a brief discussion of the detailed types of node.js middleware. For more information about node.js middleware, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Example of using Nginx reverse proxy to go-fastdfs
>>: Detailed explanation of Nginx http resource request limit (three methods)
dig - DNS lookup utility When a domain name acces...
In Ubuntu, you often encounter the situation wher...
Create table & create index create table tbl1...
Requirements: Remove HTTP response headers in IIS...
MySQL filtering timing of where conditions and ha...
Preface: I have always wanted to know how a SQL s...
Table of contents 1. List traversal 2. The role o...
.NET SDK Download Link https://dotnet.microsoft.c...
What is a table? It is composed of cell cells. In...
The EXPLAIN statement is introduced in MySQL quer...
1. [admin@JD ~]$ cd opt #Enter opt in the root di...
Preface Hello everyone, this is the CSS wizard - ...
1. Force no line break and end with an ellipsis. C...
CSS issues about background gradient and automati...
Preface ActiveMQ is the most popular and powerful...