Comments (18)
just add another middleware to set this header in your app?
from serve-static.
Good point.
from serve-static.
Wait wait wait. This is something that should be handled in this middleware. Consider the case when a 404 is returned. Only this middleware knows it's a 404 and not a 200. The header should only be set when the response is 200.
Also, only this middleware knows what filename to set for the header.
from serve-static.
All good points :) BUT this middleware is actually passing off all the resolution, filename lookup, etc. to send
, so it probably needs to be an option there first so we can pass the option to send
.
from serve-static.
From the user of this API's point of view, send doesn't exist, right? So it seems to me that one of these two things is true:
- This feature request is valid and the issue should remain open until it is solved (even if it is solved in a dependency), or
- This issue is a "won't fix" and should be closed.
from serve-static.
Sure, I mean, it seems valid for me (to serve up a bunch of files that you want to always download).
from serve-static.
Something like this would do it:
app.use(function(req, res, next) {
var writeHead = res.writeHead
res.writeHead = function(status) {
res.writeHead = writeHead
if (status === 200) {
res.header('Content-Disposition', 'attachment; filename='
+ require('path').basename(req.url))
}
res.writeHead.apply(res, arguments)
}
next()
})
Monkey-patching FTW!
from serve-static.
I'd murder a school bus full of children before I'd write that code.
from serve-static.
I guess there's no accounting for taste. :)
Still, it looks better than a metric ton of different options for every use-case.
For example, if you want to serve only ".avi" files with content-disposition, would you request another option for serve-static specifying file extension? In the example above I can do it changing just one line of code:
if (status === 200 && path.extname(req.url) === '.avi') {
from serve-static.
Well...that escalated quickly.
On Apr 3, 2014 5:41 PM, "Andrew Kelley" [email protected] wrote:
I'd murder a school bus full of children before I'd write that code.
Reply to this email directly or view it on GitHubhttps://github.com//issues/5#issuecomment-39508580
.
from serve-static.
Well...that escalated quickly.
I hope it's clear I'm exaggerating in order to entertain and amuse :-)
Still, it looks better than a metric ton of different options for every use-case.
One way would be for the API user to supply a function callback.
function (response, next) {
}
from serve-static.
I'd murder a school bus full of children before I'd write that code.
lol most of the repos in this org overwrite res.writeHead()
like that :(
poor children!
from serve-static.
@andrewrk you can use https://github.com/expressjs/on-headers and pretend like you are not overwriting writeHead
now.
from serve-static.
I'm closing this. Adding the header won't quick work, since you can't disable the conditional GET behavior. You may be better off using send
directly. In addition, the 404 argument isn't quite valid, because this middleware will next()
on 404, so this should work:
var serve = require('serve-static')(__dirname + '/public')
function cdMiddleware(req, res, next){
res.setHeader('Content-Disposition', 'attachment')
serve(req, res, function (err) {
res.removeHeader('Content-Disposition')
next(err)
})
}
from serve-static.
Ah that code snippet is beautiful. Thanks :)
Solved my issue: andrewrk/groovebasin#173
from serve-static.
I'm going to re-open this because I don't particularly like the solution I provided, haha. There should be a way to "react" to the req, res
as soon as this module decides to actually serve a file instead of doing the weird wrapping thing.
from serve-static.
With the next version, the snippet becomes this:
var serve = require('serve-static')(__dirname + '/public', {
setHeaders: function (res) { res.setHeader('Content-Disposition', 'attachment') }
})
from serve-static.
Nice, looks good!
from serve-static.
Related Issues (20)
- Need support node >= v10.x HOT 1
- how to prevent a file from being served HOT 2
- Add option to remove trailing slashes, as opposed to adding them HOT 9
- Support gzip static serving HOT 8
- Please add types for typescript HOT 1
- FR: Ability to always call next() HOT 8
- Extensions vs Directory HOT 1
- feature request: enable mapping url to a different file via a callback HOT 1
- Question: Is it possible to access the origin from setHeaders()? HOT 4
- Issue with Example Code HOT 4
- Make 'immutable' cache-control value HTTP/3 QPACK compatible HOT 1
- Use seconds instead of milliseconds for maxAge HOT 2
- 404 returned insted of 403 when uisng the dotfiles with "deny option" HOT 8
- setHeaders: need to access req.query HOT 2
- Corrupted file served if file is changed during serving. HOT 4
- Omitted `Content-Type` header when passing index file HOT 2
- Angular 13 SSR serve is failing with error - TypeError: Right-hand side of 'instanceof' is not an object HOT 5
- How to disable range requests in Next.js ? HOT 1
- Cannot use namespace 'HttpError' as a type HOT 2
- When the index is set to false, requests ending with / are being served with the content type header set to application/octet-stream instead of actual file's content type HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serve-static.