
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
express-api-require
Advanced tools
基于express的请求接口调用,把 *.json 定向到 *.js 里,以中间件形式写响应代码~
npm install express-api-require
app.js
const express = require('express')
const app = express()
const api = require('express-api-require')
// 注入
app.use(api({
root: __dirname,
process(req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
}
}))
// 托管静态文件
app.use(express.static('./'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
/api/api.js
module.exports = (req, res, next) => {
const data = {
status: 'ok'
}
res.end(JSON.stringify(data))
}
/api/data.json
{}
| 请求路径 | 说明 |
|---|---|
/api/data.json | 因为真实文件存在,使用 express.static 直接响应 |
/api/api.json | 因为真实文件不存在,而 api.js 存在,走中间件模式 |
/api/404.json | 因为真实文件不存在,又没有找到 404.js ,走 next() |
支持配合其他中间件形式去处理响应,如配合 res-json 支持响应 JSON 和 JSONP 数据:
const express = require('express')
const app = express()
const api = require('express-api-require')
const json = require('res-json')
// 注入 res.json, res.jsonp 方法
app.use(json())
// 注入
app.use(api({
root: __dirname
}))
| 名称 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| root | string | 请求根目录 | process.cwd() |
| filter | Function | request 过滤器,返回 false 时将直接 next() 该请求 | return true |
| extMap | Object | 请求扩展名映射 | {json: 'js'} |
| process | Function | 预处理器,当返回 false 时停止处理注意,该处理只在中间件文件或者真实 JSON 文件存在时注入 | - |
MIT
FAQs
基于express的请求接口调用
The npm package express-api-require receives a total of 6 weekly downloads. As such, express-api-require popularity was classified as not popular.
We found that express-api-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.