Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/old-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Oct 1, 2024
2 parents 22fe59f + 785384f commit ce88dc5
Show file tree
Hide file tree
Showing 21 changed files with 2,273 additions and 852 deletions.
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
language: node_js
node_js:
- "4.8"
- "5.12"
- "6.12"
- "7.10"
- "8.12"
- "10.12"
matrix:
include:
- node_js: "9"
env: "NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly"
- node_js: "10"
env: "NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly"
allow_failures:
# Allow the nightly installs to fail
- env: "NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly"
sudo: false
cache:
directories:
- node_modules
before_install:
# Skip updating shrinkwrap / lock
- "npm config set shrinkwrap false"

# Remove all non-test dependencies
- "npm rm --save-dev connect-redis"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
script:
- "npm run test-ci"
- "npm run lint"
after_script: "npm install [email protected] && cat ./coverage/lcov.info | coveralls"
6 changes: 3 additions & 3 deletions examples/mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ app.response.message = function(msg){
// log
if (!module.parent) app.use(logger('dev'));

// serve static files
app.use(express.static(path.join(__dirname, 'public')));

// session support
app.use(session({
resave: false, // don't save session if unmodified
Expand Down Expand Up @@ -75,6 +72,9 @@ app.use(function(req, res, next){
// load controllers
require('./lib/boot')(app, { verbose: !module.parent });

// serve static files
app.use(express.static(path.join(__dirname, 'public'), "/_static/"));

app.use(function(err, req, res, next){
// log it
if (!module.parent) console.error(err.stack);
Expand Down
3 changes: 2 additions & 1 deletion examples/route-separation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ if (!module.parent) {
app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, 'public')));

// General

Expand All @@ -48,6 +47,8 @@ app.put('/user/:id/edit', user.update);

app.get('/posts', post.list);

app.use(express.static(path.join(__dirname, 'public'), "/static/"));

/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
Expand Down
92 changes: 92 additions & 0 deletions lib/middleware/serve-static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*!
* serve-static
* Copyright(c) 2010 Sencha Inc.
* Copyright(c) 2011 TJ Holowaychuk
* Copyright(c) 2014-2016 Douglas Christopher Wilson
* MIT Licensed
*/

'use strict'

/**
* Module dependencies.
* @private
*/

var parseUrl = require('parseurl')
var resolve = require('path').resolve
var fs = require('fs')

/**
* Module exports.
* @public
*/

module.exports = serveStatic

/**
* @param {string} root
* @param {object} [options]
* @return {function}
* @public
*/

function serveStatic (root, urlRoot, opts) {
if (!root) {
throw new TypeError('root path required')
}

if (typeof root !== 'string') {
throw new TypeError('root path must be a string')
}

if(!opts) opts = {}

// fall-though
var fallthrough = opts.fallthrough !== false

// headers listener
var setHeaders = opts.setHeaders

if (setHeaders && typeof setHeaders !== 'function') {
throw new TypeError('option setHeaders must be function')
}

// setup options for send
root = resolve(root)

return function serveStatic (req, res, next) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
if (fallthrough) {
return next()
}

// method not allowed
res.statusCode = 405
res.setHeader('Allow', 'GET, HEAD')
res.setHeader('Content-Length', '0')
res.end()
return
}

var originalUrl = parseUrl.original(req)
var path = parseUrl(req).pathname

// make sure redirect occurs at mount
if (path === '/' && originalUrl.pathname.substr(-1) !== '/') {
path = ''
}

var filePath = root + "/" + path

fs.access(filePath, fs.constants.R_OK, err=>{
if(err){
next()
return
}

var urlPath = urlRoot + "/" + path
res.sendFile(urlPath);
})
}
}
116 changes: 0 additions & 116 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* @private
*/

var accepts = require('accepts');
var isIP = require('net').isIP;
var http = require('http');
var fresh = require('fresh');
Expand Down Expand Up @@ -81,97 +80,7 @@ req.header = function header(name) {
}
};

/**
* To do: update docs.
*
* Check if the given `type(s)` is acceptable, returning
* the best match when true, otherwise `undefined`, in which
* case you should respond with 406 "Not Acceptable".
*
* The `type` value may be a single MIME type string
* such as "application/json", an extension name
* such as "json", a comma-delimited list such as "json, html, text/plain",
* an argument list such as `"json", "html", "text/plain"`,
* or an array `["json", "html", "text/plain"]`. When a list
* or array is given, the _best_ match, if any is returned.
*
* Examples:
*
* // Accept: text/html
* req.accepts('html');
* // => "html"
*
* // Accept: text/*, application/json
* req.accepts('html');
* // => "html"
* req.accepts('text/html');
* // => "text/html"
* req.accepts('json, text');
* // => "json"
* req.accepts('application/json');
* // => "application/json"
*
* // Accept: text/*, application/json
* req.accepts('image/png');
* req.accepts('png');
* // => undefined
*
* // Accept: text/*;q=.5, application/json
* req.accepts(['html', 'json']);
* req.accepts('html', 'json');
* req.accepts('html, json');
* // => "json"
*
* @param {String|Array} type(s)
* @return {String|Array|Boolean}
* @public
*/

req.accepts = function(){
var accept = accepts(this);
return accept.types.apply(accept, arguments);
};

/**
* Check if the given `encoding`s are accepted.
*
* @param {String} ...encoding
* @return {String|Array}
* @public
*/

req.acceptsEncodings = function(){
var accept = accepts(this);
return accept.encodings.apply(accept, arguments);
};

/**
* Check if the given `charset`s are acceptable,
* otherwise you should respond with 406 "Not Acceptable".
*
* @param {String} ...charset
* @return {String|Array}
* @public
*/

req.acceptsCharsets = function(){
var accept = accepts(this);
return accept.charsets.apply(accept, arguments);
};

/**
* Check if the given `lang`s are acceptable,
* otherwise you should respond with 406 "Not Acceptable".
*
* @param {String} ...lang
* @return {String|Array}
* @public
*/

req.acceptsLanguages = function(){
var accept = accepts(this);
return accept.languages.apply(accept, arguments);
};

/**
* Parse Range header field, capping to the given `size`.
Expand Down Expand Up @@ -227,31 +136,6 @@ defineGetter(req, 'query', function query(){
return queryparse(querystring);
});

/**
* Check if the incoming request contains the "Content-Type"
* header field, and it contains the given mime `type`.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* req.is('html');
* req.is('text/html');
* req.is('text/*');
* // => true
*
* // When Content-Type is application/json
* req.is('json');
* req.is('application/json');
* req.is('application/*');
* // => true
*
* req.is('html');
* // => false
*
* @param {String|Array} types...
* @return {String|false|null}
* @public
*/

/**
* Return the protocol string "http" or "https"
Expand Down
Loading

0 comments on commit ce88dc5

Please sign in to comment.