
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
http-mock-middleware
Advanced tools
npm install --save http-mock-middleware
const mockMiddleware = require('http-mock-middleware')
const mockDirectory = 'mock'
app.use(mockMiddleware(mockDirectory))
/api/test.json => /api/test.json.js
module.exports = {
success: true,
msg: 'Hello, Node.js!'
}
// Using user-defined funciton (See more methods on req/res in [Express 4.x API](http://expressjs.com/en/4x/api.html#req))
module.exports = function(req, res) {
setTimeout(() => {
res.json({
success: true,
msg: 'Hello, Node.js!'
})
}, 2000)
}
// Using params in url (See more details about url pattern in [path-to-regexp](https://www.npmjs.com/package/path-to-regexp))
// /api/test/1 => /api/test/:test_id.js
module.exports = function(req, res) {
res.json({
success: true,
msg: req.params.test_id
})
}
// Simplify the response, default to return JSON format
// * Caution Please *
// If you ever call any method of res object(this second argument) synchronously
// in your function, the middleware will ignore the return of your function.
// If you call any method of res object asynchronously but return anything except
// null/undefined at the same time, the return value will served as the result.
// So please use plain object for return or call specific method on res object.
// Do not mixing them up!
module.exports = function(req) {
return {
success: true,
msg: req.params.test_id
})
}
// Define different HTTP methods
module.exports = {
__GET: {
success: true,
msg: 'Hello, Node.js!'
},
__POST: {
success: 'true'
}
}
FAQs
A Http Mock Middleware for Express
The npm package http-mock-middleware receives a total of 22 weekly downloads. As such, http-mock-middleware popularity was classified as not popular.
We found that http-mock-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.