Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
applejian committed Nov 16, 2021
1 parent f871106 commit 57747be
Show file tree
Hide file tree
Showing 9 changed files with 1,242 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 4/express/myapp-async/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const express = require('express')
const app = express()
const port = 3000

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
/**
* 中间件 1
*/
app.use(async (req, res, next) => {
console.log('first');
await next();
console.log('first end');
});
/**
* 中间件 2
*/
app.use(async (req, res, next) => {
console.log('second');
await next();
console.log('second end');
});

/**
* 异步中间件
*/
app.use(async (req, res, next) => {
console.log('async');
await next();
await new Promise(
(resolve) =>
setTimeout(
() => {
console.log(`wait ${s} ms end`);
resolve()
},
1000)
);
console.log('async end');
});

/**
* 中间件 3
*/
app.use(async (req, res, next) => {
console.log('third');
await next();
console.log('third end');
});

app.get('/', (req, res) => res.send('Hello World!'))

Loading

0 comments on commit 57747be

Please sign in to comment.