Socket
Socket
Sign inDemoInstall

serve-static

Package Overview
Dependencies
10
Maintainers
6
Versions
61
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.3 to 1.3.0

8

History.md

@@ -0,1 +1,9 @@

1.3.0 / 2014-06-28
==================
* Add `setHeaders` option
* Include HTML link in redirect response
* deps: send@0.5.0
- Accept string for `maxAge` (converted by `ms`)
1.2.3 / 2014-06-11

@@ -2,0 +10,0 @@ ==================

56

index.js

@@ -62,2 +62,10 @@ /*!

// headers listener
var setHeaders = options.setHeaders
delete options.setHeaders
if (setHeaders && typeof setHeaders !== 'function') {
throw new TypeError('option setHeaders must be function')
}
// setup options for send

@@ -73,25 +81,39 @@ options.maxage = options.maxage || options.maxAge || 0;

if (path == '/' && originalUrl.pathname[originalUrl.pathname.length - 1] != '/') {
return directory();
if (path === '/' && originalUrl.pathname[originalUrl.pathname.length - 1] !== '/') {
// make sure redirect occurs at mount
path = ''
}
function directory() {
if (!redirect) return next();
var target;
originalUrl.pathname += '/';
target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
res.end('Redirecting to ' + escapeHtml(target));
// create send stream
var stream = send(req, path, opts)
if (redirect) {
// redirect relative to originalUrl
stream.on('directory', function redirect() {
originalUrl.pathname += '/'
var target = url.format(originalUrl)
res.statusCode = 303
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Location', target)
res.end('Redirecting to <a href="' + escapeHtml(target) + '">' + escapeHtml(target) + '</a>\n')
})
} else {
// forward to next middleware on directory
stream.on('directory', next)
}
function error(err) {
if (404 == err.status) return next();
next(err);
// add headers listener
if (setHeaders) {
stream.on('headers', setHeaders)
}
send(req, path, opts)
.on('error', error)
.on('directory', directory)
.pipe(res);
// forward non-404 errors
stream.on('error', function error(err) {
next(err.status === 404 ? null : err)
})
// pipe
stream.pipe(res)
};

@@ -98,0 +120,0 @@ };

{
"name": "serve-static",
"description": "Serve static files",
"version": "1.2.3",
"version": "1.3.0",
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",

@@ -11,6 +11,6 @@ "license": "MIT",

"parseurl": "1.0.1",
"send": "0.4.3"
"send": "0.5.0"
},
"devDependencies": {
"istanbul": "0.2.10",
"istanbul": "0.2.13",
"mocha": "~1.20.0",

@@ -17,0 +17,0 @@ "should": "~4.0.0",

@@ -31,4 +31,5 @@ # serve-static

- `index` Default file name, defaults to `'index.html'`
- `maxAge` Browser cache maxAge in milliseconds. defaults to `0`
- `maxAge` Browser cache maxAge in milliseconds. This can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme) module. defaults to `0`
- `redirect` Redirect to trailing "/" when the pathname is a dir. defaults to `true`
- `setHeaders` Function to set custom headers on response.

@@ -69,2 +70,21 @@ ## Examples

### Serve all files as downloads
```js
var express = require('express')
var serveStatic = require('serve-static')
var app = express()
app.use(serveStatic('public/ftp', {
'index': false,
'setHeaders': setHeaders
}))
app.listen(3000)
function setHeaders(res, path) {
res.attachment(path)
}
```
## License

@@ -71,0 +91,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc