diff --git a/README.md b/README.md index cf79a36..6130ffa 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 @@ -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', }); ``` diff --git a/examples/pico-http-server.js b/examples/pico-http-server.js index 5818322..60a7b9a 100644 --- a/examples/pico-http-server.js +++ b/examples/pico-http-server.js @@ -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' diff --git a/examples/pico-https-server.js b/examples/pico-https-server.js index 608b935..dc3c598 100644 --- a/examples/pico-https-server.js +++ b/examples/pico-https-server.js @@ -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', }); diff --git a/index.js b/index.js index 70bd126..3f88aad 100644 --- a/index.js +++ b/index.js @@ -59,25 +59,25 @@ 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; @@ -85,7 +85,7 @@ const getMimeType = (url) => MIME_TYPE_MAP[path.parse(url).ext] || DEFAULT_MIME_ /** * Get if file is directory * - * @param url + * @param {string} url * @returns {boolean} */ const getIsDirectory = (url) => fs.statSync(url).isDirectory(); @@ -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)) { @@ -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 }); } } @@ -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); diff --git a/package.json b/package.json index 4cdce16..a66a8de 100644 --- a/package.json +++ b/package.json @@ -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",