@macropygia/vite-plugin-connect-middleware
v0.0.9
Published
Vite plugin to configure middleware for dev and preview servers
Downloads
147
Maintainers
Readme
@macropygia/vite-plugin-connect-middleware
English | 日本語
Vite plugin to configure middleware for dev and preview servers.
- This package is currently unstable.
- Breaking changes may occur without any notice, even if in patch releases.
- See CHANGELOG for changes.
- This package only works as ESM.
"type": "module"
is required.
Usage
Both servers use the same middleware
// vite.config.js
import { defineConfig } from 'vite'
import vitePluginConnectMiddleware from '@macropygia/vite-plugin-connect-middleware'
const reDownloads = /^\/downloads\/(?<dir>sound|movie)\/(?<path>.*)$/
export default defineConfig({
plugins: [
vitePluginConnectMiddleware(
(req, res, next) => {
if (req.url) {
if (req.url.startsWith('/app/')) {
req.url = '/app/'
} else if (reDownloads.test(req.url)) {
const reResult = req.url.match(reDownloads)
res.writeHead(301, {
Location: `https://example.com/downloads/${
reResult.groups['dir']
}/${reResult.groups['path']}`,
})
}
}
return next()
}
),
],
})
Servers use different middleware
vitePluginConnectMiddleware({
// for dev server
dev: (req, res, next) => {
// ...
},
// for preview server
preview: (req, res, next) => {
// ...
},
}),
dev
andpreview
are optional