Skip to content

Commit 0b84965

Browse files
committed
init from connect
0 parents  commit 0b84965

File tree

14 files changed

+719
-0
lines changed

14 files changed

+719
-0
lines changed

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Thumbs.db
34+
35+
# Node.js #
36+
###########
37+
lib-cov
38+
*.seed
39+
*.log
40+
*.csv
41+
*.dat
42+
*.out
43+
*.pid
44+
*.gz
45+
46+
pids
47+
logs
48+
results
49+
50+
node_modules
51+
npm-debug.log
52+
53+
# Git #
54+
#######
55+
*.orig
56+
*.BASE.*
57+
*.BACKUP.*
58+
*.LOCAL.*
59+
*.REMOTE.*
60+
61+
# Components #
62+
##############
63+
64+
/build
65+
/components

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "0.8"
4+
- "0.10"

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2010 Sencha Inc.
4+
Copyright (c) 2011 LearnBoost
5+
Copyright (c) 2011 TJ Holowaychuk
6+
Copyright (c) 2014 Douglas Christopher Wilson
7+
8+
Permission is hereby granted, free of charge, to any person obtaining
9+
a copy of this software and associated documentation files (the
10+
'Software'), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be
17+
included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
20+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Serve Static
2+
3+
Previously `connect.static()`.
4+
5+
Usage:
6+
7+
```js
8+
var connect = require('connect');
9+
var serveStatic = require('serve-static');
10+
11+
var app = connect();
12+
13+
app.use(serveStatic('public/ftp', {'index': 'default.html'}));
14+
app.listen();
15+
```
16+
17+
## License
18+
19+
The MIT License (MIT)
20+
21+
Copyright (c) 2014 Douglas Christopher Wilson
22+
23+
Permission is hereby granted, free of charge, to any person obtaining a copy
24+
of this software and associated documentation files (the "Software"), to deal
25+
in the Software without restriction, including without limitation the rights
26+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27+
copies of the Software, and to permit persons to whom the Software is
28+
furnished to do so, subject to the following conditions:
29+
30+
The above copyright notice and this permission notice shall be included in
31+
all copies or substantial portions of the Software.
32+
33+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39+
THE SOFTWARE.

index.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*!
2+
* Connect - static
3+
* Copyright(c) 2010 Sencha Inc.
4+
* Copyright(c) 2011 TJ Holowaychuk
5+
* Copyright(c) 2014 Douglas Christopher Wilson
6+
* MIT Licensed
7+
*/
8+
9+
/**
10+
* Module dependencies.
11+
*/
12+
13+
var send = require('send');
14+
var url = require('url');
15+
16+
/**
17+
* Static:
18+
*
19+
* Static file server with the given `root` path.
20+
*
21+
* Examples:
22+
*
23+
* var oneDay = 86400000;
24+
* var serveStatic = require('serve-static');
25+
*
26+
* connect()
27+
* .use(serveStatic(__dirname + '/public'))
28+
*
29+
* connect()
30+
* .use(serveStatic(__dirname + '/public', { maxAge: oneDay }))
31+
*
32+
* Options:
33+
*
34+
* - `maxAge` Browser cache maxAge in milliseconds. defaults to 0
35+
* - `hidden` Allow transfer of hidden files. defaults to false
36+
* - `redirect` Redirect to trailing "/" when the pathname is a dir. defaults to true
37+
* - `index` Default file name, defaults to 'index.html'
38+
*
39+
* @param {String} root
40+
* @param {Object} options
41+
* @return {Function}
42+
* @api public
43+
*/
44+
45+
exports = module.exports = function(root, options){
46+
options = options || {};
47+
48+
// root required
49+
if (!root) throw new TypeError('root path required');
50+
51+
// default redirect
52+
var redirect = false !== options.redirect;
53+
54+
return function staticMiddleware(req, res, next) {
55+
if ('GET' != req.method && 'HEAD' != req.method) return next();
56+
var originalUrl = url.parse(req.originalUrl);
57+
var path = parse(req).pathname;
58+
59+
if (path == '/' && originalUrl.pathname[originalUrl.pathname.length - 1] != '/') {
60+
return directory();
61+
}
62+
63+
function directory() {
64+
if (!redirect) return next();
65+
var target;
66+
originalUrl.pathname += '/';
67+
target = url.format(originalUrl);
68+
res.statusCode = 303;
69+
res.setHeader('Location', target);
70+
res.end('Redirecting to ' + escape(target));
71+
}
72+
73+
function error(err) {
74+
if (404 == err.status) return next();
75+
next(err);
76+
}
77+
78+
send(req, path)
79+
.maxage(options.maxAge || 0)
80+
.root(root)
81+
.index(options.index || 'index.html')
82+
.hidden(options.hidden)
83+
.on('error', error)
84+
.on('directory', directory)
85+
.pipe(res);
86+
};
87+
};
88+
89+
/**
90+
* Escape the given string of `html`.
91+
*
92+
* @param {String} html
93+
* @return {String}
94+
* @api private
95+
*/
96+
97+
function escape(html) {
98+
return String(html)
99+
.replace(/&(?!\w+;)/g, '&')
100+
.replace(/</g, '&lt;')
101+
.replace(/>/g, '&gt;')
102+
.replace(/"/g, '&quot;');
103+
};
104+
105+
/**
106+
* Parse the `req` url.
107+
*
108+
* @param {ServerRequest} req
109+
* @return {Object}
110+
* @api private
111+
*/
112+
113+
function parse(req) {
114+
var parsed = url.parse(req.url);
115+
116+
if (parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) {
117+
// This parses pathnames, and a strange pathname like //r@e should work
118+
parsed = url.parse(req.url.replace(/@/g, '%40'));
119+
}
120+
121+
return parsed;
122+
};

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "serve-static",
3+
"description": "Serve static files",
4+
"version": "1.0.0",
5+
"author": "Douglas Christopher Wilson <[email protected]>",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/expressjs/serve-static.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/expressjs/serve-static/issues"
13+
},
14+
"dependencies": {
15+
"send": "0.1.4"
16+
},
17+
"devDependencies": {
18+
"connect": "^2.13.0",
19+
"mocha": "^1.17.0",
20+
"should": "^3.0.0",
21+
"supertest": "~0.9.0"
22+
},
23+
"engines": {
24+
"node": ">= 0.8.0"
25+
},
26+
"scripts": {
27+
"test": "mocha --reporter spec --require should"
28+
}
29+
}

test/fixtures/.hidden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am hidden

test/fixtures/foo bar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baz

test/fixtures/nums

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123456789

0 commit comments

Comments
 (0)