Skip to content

Commit

Permalink
Readme update + some naming and jsdoc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
udi committed Jun 6, 2018
1 parent 5ef5bd2 commit 421f77f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Pico-static-server
Tiny yet fully functional Node.js static files server with zero dependencies and **HTTPS** support.
Tiny yet fully functional Node.js static files server with zero dependencies and *HTTPS* support.

### Install
Via npm:
Expand All @@ -16,10 +16,7 @@ node ./examples/pico-http-server.js
```
assuming you've put your content into ``./examples/static/`` folder.

**HTTPS** example requires you to generate SSL cerificates first:
```bash
cd ./examples
```
*HTTPS* example requires you to generate SSL cerificates first:
Generate 2048-bit RSA private key and remove the password from generated key
```bash
openssl genrsa -des3 -passout pass:x -out localhost.pem 2048 && openssl rsa -passin pass:x -in localhost.pem -out localhost.key && rm localhost.pem
Expand Down Expand Up @@ -51,17 +48,17 @@ You may want to create your own static HTTP server:
port: 8080, // defaults to 8080
});
```
or even HTTPS one:
or even *HTTPS* one:
```javascript
const createServer = require('pico-static-server');

const staticServer = createServer({
defaultFile: 'index.html', // defaults to 'index.html'
staticPath: './static', // defaults to './'
staticPath: __dirname + '/static', // defaults to './'
port: 8080, // defaults to 8080
protocol: 'https', // defaults to 'http'
cert: __dirname + '/localhost.crt',
key: __dirname + 'localhost.key',
key: __dirname + '/localhost.key',
});
```

Expand Down
2 changes: 1 addition & 1 deletion examples/pico-http-server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createServer = require('pico-static-server');
const createServer = require('./../index.js');

const staticServer = createServer({
defaultFile: 'defaultfile.html', // defaults to 'index.html'
Expand Down
10 changes: 5 additions & 5 deletions examples/pico-https-server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const createServer = require('pico-static-server');
const createServer = require('./../index.js');

const staticServer = createServer({
defaultFile: 'defaultfile.html', // defaults to 'index.html'
staticPath: __dirname + '/static', // defaults to './'
port: 8080, // defaults to 8080
protocol: 'https', // defaults to 'http'
defaultFile: 'defaultfile.html', // defaults to 'index.html'
staticPath: __dirname + '/static', // defaults to './'
port: 8080, // defaults to 8080
protocol: 'https', // defaults to 'http'
cert: __dirname + '/localhost.crt',
key: __dirname + '/localhost.key',
});
Expand Down
35 changes: 17 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,33 @@ const UNKNOWN_METHOD_RESPONSE_HEADERS = { Allow: 'GET, HEAD', 'Content-Length':
/**
* Perform http response
*
* @param {Object} res
* @param {Object} response
* @param {number} code
* @param {Object} headers
* @param {string|Buffer} [data]
*/
const respond = (res, code, headers, data) => {
res.writeHead(code, headers, http.STATUS_CODES[code]);
const respond = (response, code, headers, data) => {
response.writeHead(code, headers, http.STATUS_CODES[code]);

if (typeof data !== 'undefined') {
res.write(data);
response.write(data);
}

res.end();
response.end();
};

/**
* Get mime type according to request path
*
* @param url
* @param {string} url
* @returns {string}
*/
const getMimeType = (url) => MIME_TYPE_MAP[path.parse(url).ext] || DEFAULT_MIME_TYPE;

/**
* Get if file is directory
*
* @param url
* @param {string} url
* @returns {boolean}
*/
const getIsDirectory = (url) => fs.statSync(url).isDirectory();
Expand All @@ -99,19 +99,18 @@ const getIsDirectory = (url) => fs.statSync(url).isDirectory();
const createServer = (customOptions) => {
const options = { ...DEFAULT_OPTIONS, ...customOptions };

const responseHandler = (req, res) => {
if (req.method === 'OPTIONS') {
respond(res, 200, UNKNOWN_METHOD_RESPONSE_HEADERS);
const responseHandler = (request, response) => {
if (request.method === 'OPTIONS') {
respond(response, 200, UNKNOWN_METHOD_RESPONSE_HEADERS);
return;
}

if (req.method !== 'GET' && req.method !== 'HEAD') {
respond(res, 405, UNKNOWN_METHOD_RESPONSE_HEADERS);
if (request.method !== 'GET' && request.method !== 'HEAD') {
respond(response, 405, UNKNOWN_METHOD_RESPONSE_HEADERS);
return;
}

const parsedUrl = url.parse(req.url);
let requestPath = path.join(options.staticPath, parsedUrl.pathname);
let requestPath = path.join(options.staticPath, path.normalize(url.parse(request.url).pathname));

if (fs.existsSync(requestPath)) {
if (getIsDirectory(requestPath)) {
Expand All @@ -121,12 +120,12 @@ const createServer = (customOptions) => {
const data = fs.readFileSync(requestPath);

if (data instanceof Error) {
respond(res, 500, { 'Content-Length': 0 });
respond(response, 500, { 'Content-Length': 0 });
} else {
respond(res, 200, { 'Content-type': getMimeType(requestPath), 'Content-length': data.length }, data);
respond(response, 200, { 'Content-type': getMimeType(requestPath), 'Content-length': data.length }, data);
}
} else {
respond(res, 404, { 'Content-Length': 0 });
respond(response, 404, { 'Content-Length': 0 });
}
}

Expand All @@ -136,7 +135,7 @@ const createServer = (customOptions) => {
return http.createServer(responseHandler).listen(options.port, listenCallback)
}

return serverHanhttps.createServer({
return https.createServer({
cert: fs.readFileSync(options.cert),
key: fs.readFileSync(options.key),
}, responseHandler).listen(options.port, listenCallback);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pico-static-server",
"version": "3.0.1",
"version": "3.0.2",
"description": "Small yet fully functional Node.js static files server with zero dependencies with HTTPS support",
"main": "index.js",
"module": "index.js",
Expand Down

0 comments on commit 421f77f

Please sign in to comment.