# serve-static-callback

> Work exactly like [Express static middleware](https://expressjs.com/en/resources/middleware/serve-static.html), but add a callback function.

Latest version **1.0.2** (published 2022-05-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install serve-static-callback
pnpm add serve-static-callback
yarn add serve-static-callback
bun add serve-static-callback
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-05-13 |
| First published | 2022-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | cestoliv |
| Maintainers | cestoliv |
| Keywords | express, typescript, express-middleware, expressjs, serve-static |

## Links

- npm: https://www.npmjs.com/package/serve-static-callback
- Repository: https://git.chevro.fr/cestoliv/serve-static-callback
- npm.io page: https://npm.io/package/serve-static-callback

## Dependencies (12)

- [send](https://npm.io/package/send.md) ^0.18.0
- [express](https://npm.io/package/express.md) ^4.18.1
- [parseurl](https://npm.io/package/parseurl.md) ^1.3.3
- [encodeurl](https://npm.io/package/encodeurl.md) ^1.0.2
- [@types/send](https://npm.io/package/@types/send.md) ^0.17.1
- [escape-html](https://npm.io/package/escape-html.md) ^1.0.3
- [on-finished](https://npm.io/package/on-finished.md) ^2.4.1
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.13
- [@types/parseurl](https://npm.io/package/@types/parseurl.md) ^1.3.1
- [@types/encodeurl](https://npm.io/package/@types/encodeurl.md) ^1.0.0
- [@types/escape-html](https://npm.io/package/@types/escape-html.md) ^1.0.2
- [@types/on-finished](https://npm.io/package/@types/on-finished.md) ^2.3.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-05-13
- 1.0.1 — 2022-05-04
- 1.0.0 — 2022-05-04

## README

# Express static middleware with a callback

Work exactly like [Express static middleware](https://expressjs.com/en/resources/middleware/serve-static.html), but add a callback function.

The callback function is called when a file is returned to the client. IT IS NOT called, if no file is found, nor if a redirection has been performed
# Install
```bash
npm install serve-static-callback
```
# API
```javascript
import serveStaticCb from 'serve-static-callback'
```
## serveStaticCb(root, options, callback)
Create a middleware function, just like Express built-in serve-static.

### Options
The full list of options is on [the official server-static doc](https://expressjs.com/en/resources/middleware/serve-static.html).

### Callback
The function that you want to be executed after the file beeing served.
```javascript
function (req: express.Request, res: express.Response, path: string) {

}
```

## Instead of
```typescript
import express from 'express'

const app = express()
app.use(express.static('public', { dotfiles: "deny" }))
app.listen(80)
```

## You can now
```typescript
import express from 'express'
import serveStaticCb from 'serve-static-callback'

const app = express()
app.use(serveStaticCb('public', { dotfiles: "deny" },
	function (req: express.Request, res: express.Response, path: string) {
		console.log(`A request to ${req.path} has been made, the file ${path} has been served !`)
		// e.g. => A request to / has been made, the file public/index.html has been served !
	})
)
app.listen(80)
```

---
_Source: https://npm.io/package/serve-static-callback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
