https://github.com/drawveloper/connect-tryfiles
nginx try_files style connect middleware: serve local file if exists or proxy to address
https://github.com/drawveloper/connect-tryfiles
Last synced: over 1 year ago
JSON representation
nginx try_files style connect middleware: serve local file if exists or proxy to address
- Host: GitHub
- URL: https://github.com/drawveloper/connect-tryfiles
- Owner: drawveloper
- License: mit
- Created: 2014-05-01T13:27:00.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2015-04-04T13:34:09.000Z (about 11 years ago)
- Last Synced: 2025-02-28T14:05:43.460Z (over 1 year ago)
- Language: CoffeeScript
- Homepage:
- Size: 190 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
connect-tryfiles
================
# WARNING: DEPRECATED
This was a nice experiment, but right now there is a better way to accomplish the same.
As the new `serve-static` middleware from Express now calls `next` when it doesn't find a file, you can simply add a proxy afterwards:
connect = require 'connect'
http = require 'http'
url = require 'url'
serveStatic = require 'serve-static'
proxy = require 'proxy-middleware'
app = connect()
.use(serveStatic('./files'))
.use(proxy(url.parse('https://example.com/endpoint')))
server = http.createServer(app).listen(8000)
Here be dragons! You have been warned :)
### Description

nginx try_files style connect middleware: serve local file if exists or proxy to address
### Idea
If a local file is available at the path corresponding to the URL, this middleware will do nothing (giving the chance to some other middleware serve it).
Else, it will proxy the request to the given target.
This way, you can serve local files easily and proxy the rest to your remote server.
### Usage
connect = require 'connect'
http = require 'http'
tryfiles = require 'connect-tryfiles'
app = connect()
.use(tryfiles('**', 'http://localhost:9000', {cwd: 'files'}))
.use(connect.static('./files'))
server = http.createServer(app).listen(8000)
Assuming there is
- a file at `./files/foo`
- a different server listening at localhost:9000 that returns "world" to any request
Requests to:
- `http://localhost:8000/foo` will retrieve the file at `files/foo`
- `http://localhost:8000/hello` will return "world"