Created
December 1, 2019 09:24
-
-
Save netputer/5fda6b8465263ed19bf1b689276263b9 to your computer and use it in GitHub Desktop.
Express HTTP Context Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "express-context", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"dependencies": { | |
"express": "^4.17.1", | |
"express-http-context": "^1.2.3", | |
"request": "^2.88.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const httpContext = require('express-http-context') | |
const UserService = require('./service-user') | |
const app = express() | |
app.use(httpContext.middleware) | |
app.get('/:id', (req, res) => { | |
httpContext.set('id', req.params.id) | |
UserService.getUser().then((result) => { | |
res.json(result) | |
}) | |
}) | |
app.listen(3000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const httpContext = require('express-http-context') | |
function getRandomTimeout() { | |
return 0 | |
return Math.floor(Math.random() * 5000) | |
} | |
function getUser() { | |
const timeout = getRandomTimeout() | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve({ | |
id: httpContext.get('id'), | |
timeout, | |
now: Date.now(), | |
}) | |
}, timeout) | |
}) | |
} | |
module.exports = { | |
getUser, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request') | |
const startTime = Date.now() | |
function send(id) { | |
return new Promise((resolve, reject) => { | |
request({ | |
uri: 'http://127.0.0.1:3000/' + id, | |
json: true | |
}, (err, res, data) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(data) | |
}) | |
}) | |
} | |
let counter = 100000 | |
console.log('TASK BEGIN') | |
while (counter) { | |
counter-- | |
let i = Math.floor(Math.random() * counter) + '' | |
send(i).then((data) => { | |
if (data.id !== i) { | |
const newError = new Error('WARNING') | |
newError.npType = 666 | |
throw newError | |
} | |
// console.log('While Success', `Cost ${Date.now() - startTime}ms`, i) | |
}).catch((err) => { | |
if (1 || err.npType === 666) { | |
console.log('While Error', `Cost ${Date.now() - startTime}ms`, err.npType) | |
} | |
}) | |
} | |
console.log('TASK END', Date.now() - startTime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment