| <h1>Im Single!</h1> |
+1
-0
@@ -15,2 +15,3 @@ const path = require('path'), | ||
| this.chain = []; | ||
| this.staticFiles = []; | ||
@@ -17,0 +18,0 @@ this.server = http.createServer(); |
+42
-20
@@ -11,27 +11,49 @@ /* | ||
| module.exports = function() { | ||
| function processor(req, res, next) { | ||
| if(this.route && this.route !== req.path) { | ||
| return next(); | ||
| } | ||
| let file = this.path; | ||
| if(!this.route) { | ||
| file = path.join(this.path, | ||
| (req.path === '/' ? './index.html' : req.path)); | ||
| } | ||
| //console.log(file); | ||
| const pathPieces = arguments; | ||
| fs.access(file, fs.constants.F_OK | fs.constants.R_OK, err => { | ||
| if(!err) { | ||
| res.setHeader( | ||
| 'content-type', | ||
| CONTENT_TYPES[ | ||
| path.extname(file).substring(1).toLowerCase() | ||
| ] || 'text/plain'); | ||
| return function(req, res, next) { | ||
| const file = path.join( | ||
| ...pathPieces, | ||
| (req.path === '/' ? './index.html' : req.path)); | ||
| fs.createReadStream(file).pipe(res); | ||
| fs.access(file, fs.constants.F_OK | fs.constants.R_OK, err => { | ||
| if(!err) { | ||
| res.setHeader( | ||
| 'content-type', | ||
| CONTENT_TYPES[ | ||
| path.extname(file).substring(1).toLowerCase() | ||
| ] || 'text/plain'); | ||
| next(true); | ||
| } else { | ||
| next(); | ||
| } | ||
| }); | ||
| }; | ||
| module.exports = function(...pathPieces) { | ||
| const ext = path.extname(pathPieces[pathPieces.length - 1]); | ||
| fs.createReadStream(file).pipe(res); | ||
| // If there is no file extension its a static folder | ||
| let ctx = { | ||
| route: null, | ||
| path: path.join(...pathPieces) | ||
| }; | ||
| next(true); | ||
| } else { | ||
| next(); | ||
| } | ||
| }); | ||
| }.bind(path); | ||
| if(ext) { | ||
| ctx = { | ||
| route: pathPieces[0], | ||
| path: path.join(...pathPieces.slice(1)) | ||
| }; | ||
| } | ||
| return processor.bind(ctx); | ||
| }; |
+1
-1
| { | ||
| "name": "rece", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "description": "Request, Response, Repeat.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+15
-3
@@ -94,5 +94,5 @@ const fs = require('fs'), | ||
| expect(srv.chain[1]).toBe(p1); | ||
| expect(srv.chain[2]).toBe(p2); | ||
| expect(srv.chain[3]).toBe(p3); | ||
| expect(srv.chain[2]).toBe(p1); | ||
| expect(srv.chain[3]).toBe(p2); | ||
| expect(srv.chain[4]).toBe(p3); | ||
| }); | ||
@@ -138,3 +138,15 @@ | ||
| srv.serve(__dirname, 'public'); | ||
| srv.serve('/single/file', __dirname, 'single.html'); | ||
| it('should be able to serve single files', () => { | ||
| return request({ | ||
| uri: `${urlBase}/single/file`, | ||
| resolveWithFullResponse: true | ||
| }).then((res) => { | ||
| expect(res.statusCode).toBe(200); | ||
| expect(res.headers['content-type']).toBe('text/html'); | ||
| expect(res.body).toBe('<h1>Im Single!</h1>'); | ||
| }); | ||
| }); | ||
| it('should serve html files', () => { | ||
@@ -141,0 +153,0 @@ return request({ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
61370
1.45%32
3.23%647
4.86%