Git Product home page Git Product logo

Comments (18)

rlidwka avatar rlidwka commented on May 23, 2024

just add another middleware to set this header in your app?

from serve-static.

andrewrk avatar andrewrk commented on May 23, 2024

Good point.

from serve-static.

andrewrk avatar andrewrk commented on May 23, 2024

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.

dougwilson avatar dougwilson commented on May 23, 2024

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.

andrewrk avatar andrewrk commented on May 23, 2024

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:

  1. This feature request is valid and the issue should remain open until it is solved (even if it is solved in a dependency), or
  2. This issue is a "won't fix" and should be closed.

from serve-static.

dougwilson avatar dougwilson commented on May 23, 2024

Sure, I mean, it seems valid for me (to serve up a bunch of files that you want to always download).

from serve-static.

rlidwka avatar rlidwka commented on May 23, 2024

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.

andrewrk avatar andrewrk commented on May 23, 2024

I'd murder a school bus full of children before I'd write that code.

from serve-static.

rlidwka avatar rlidwka commented on May 23, 2024

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.

defunctzombie avatar defunctzombie commented on May 23, 2024

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.

andrewrk avatar andrewrk commented on May 23, 2024

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.

jonathanong avatar jonathanong commented on May 23, 2024

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.

dougwilson avatar dougwilson commented on May 23, 2024

@andrewrk you can use https://github.com/expressjs/on-headers and pretend like you are not overwriting writeHead now.

from serve-static.

dougwilson avatar dougwilson commented on May 23, 2024

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.

andrewrk avatar andrewrk commented on May 23, 2024

Ah that code snippet is beautiful. Thanks :)

Solved my issue: andrewrk/groovebasin#173

from serve-static.

dougwilson avatar dougwilson commented on May 23, 2024

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.

dougwilson avatar dougwilson commented on May 23, 2024

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.

andrewrk avatar andrewrk commented on May 23, 2024

Nice, looks good!

from serve-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.