npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-api-require

v0.0.2

Published

基于express的请求接口调用

Downloads

5

Readme

express-api-require

基于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 文件存在时注入 | -

License

MIT