Socket
Socket
Sign inDemoInstall

http-server

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-server - npm Package Compare versions

Comparing version 0.7.5 to 0.8.0

84

lib/http-server.js
var fs = require('fs'),
util = require('util'),
union = require('union'),
ecstatic = require('ecstatic');
ecstatic = require('ecstatic'),
httpProxy = require('http-proxy'),
corser = require('corser');

@@ -22,5 +24,3 @@ var HTTPServer = exports.HTTPServer = function (options) {

if (options.headers) {
this.headers = options.headers;
}
this.headers = options.headers || {};

@@ -37,20 +37,62 @@ this.cache = options.cache || 3600; // in seconds.

var before = options.before ? options.before.slice() : [];
before.push(function (req, res) {
if (options.logFn) {
options.logFn(req, res);
}
res.emit('next');
});
if (options.cors) {
this.headers['Access-Control-Allow-Origin'] = '*';
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept';
before.push(corser.create());
}
if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
res.setHeader('Content-Type', 'text/plain');
var robots = options.robots === true
? 'User-agent: *\nDisallow: /'
: options.robots.replace(/\\n/, '\n');
return res.end(robots);
}
res.emit('next');
});
}
before.push(ecstatic({
root: this.root,
cache: this.cache,
showDir: this.showDir,
autoIndex: this.autoIndex,
defaultExt: this.ext,
handleError: typeof options.proxy !== 'string'
}));
if (typeof options.proxy === 'string') {
var proxy = httpProxy.createProxyServer({});
before.push(function (req, res) {
proxy.web(req, res, {
target: options.proxy,
changeOrigin: true
});
});
}
var serverOptions = {
before: (options.before || []).concat([
function (req, res) {
if (options.logFn) {
options.logFn(req, res);
}
res.emit('next');
},
ecstatic({
root: this.root,
cache: this.cache,
showDir: this.showDir,
autoIndex: this.autoIndex,
defaultExt: this.ext
})
]),
headers: this.headers || {}
before: before,
headers: this.headers,
onError: function (err, req, res) {
if (options.logFn) {
options.logFn(req, res, err);
}
res.end();
}
};

@@ -57,0 +99,0 @@

{
"name": "http-server",
"version": "0.7.5",
"description": "a simple zero-configuration command-line http server",
"version": "0.8.0",
"description": "A simple zero-configuration command-line http server",
"main": "./lib/http-server",
"repository": {
"type": "git",
"url": "git://github.com/nodeapps/http-server.git"
"url": "git://github.com/indexzero/http-server.git"
},

@@ -64,5 +64,7 @@ "keywords": [

"union": "~0.4.3",
"ecstatic": "~0.6.0",
"portfinder": "0.2.x",
"opener": "~1.4.0"
"ecstatic": "~0.7.0",
"http-proxy": "^1.8.1",
"portfinder": "0.4.x",
"opener": "~1.4.0",
"corser": "~2.0.0"
},

@@ -72,3 +74,3 @@ "devDependencies": {

"request": "2.49.x",
"stylezero": "2.1.1"
"stylezero": "2.2.0"
},

@@ -84,3 +86,2 @@ "bugs": {

],
"analyze": false,
"preferGlobal": "true",

@@ -87,0 +88,0 @@ "bin": {

@@ -65,2 +65,6 @@ # http-server: a command-line http server

`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1.
`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
`-S` or `--ssl` Enable https.

@@ -72,5 +76,4 @@

`-r` or `--robots` Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')
`-h` or `--help` Print this list and exit.
`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds
(defaults to '3600'). To disable caching, use -c-1.

@@ -15,2 +15,3 @@ var assert = require('assert'),

root: root,
robots: true,
headers: {

@@ -21,2 +22,3 @@ 'Access-Control-Allow-Origin': '*',

});
server.listen(8080);

@@ -62,2 +64,10 @@ this.callback(null, server);

},
'when robots options is activated': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'should respond with status code 200 to /robots.txt': function (res) {
assert.equal(res.statusCode, 200);
}
},
'and options include custom set http-headers': {

@@ -71,4 +81,78 @@ topic: function () {

}
},
'When http-server is proxying from 8081 to 8080': {
topic: function () {
var proxyServer = httpServer.createServer({
proxy: 'http://127.0.0.1:8080/',
root: path.join(__dirname, 'fixtures')
});
proxyServer.listen(8081);
this.callback(null, proxyServer);
},
'it should serve files from the proxy server root directory': {
topic: function () {
request('http://127.0.0.1:8081/root/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of the served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
},
'it should fallback to the proxied server': {
topic: function () {
request('http://127.0.0.1:8081/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of the proxied served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
}
}
},
'When cors is enabled': {
topic: function () {
var server = httpServer.createServer({
root: root,
cors: true
});
server.listen(8082);
this.callback(null, server);
},
'and given OPTIONS request': {
topic: function () {
request({
method: 'OPTIONS',
uri: 'http://127.0.0.1:8082/',
headers: {
'Access-Control-Request-Method': 'GET',
Origin: 'http://example.com',
'Access-Control-Request-Headers': 'Foobar'
}
}, this.callback);
},
'status code should be 204': function (err, res, body) {
assert.equal(res.statusCode, 204);
}
}
}
}).export(module);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc