diff --git a/package.json b/package.json index fd310b6..3f3a26c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,4 @@ { - "type": "module", "name": "node_compression-app", "version": "1.0.0", "description": "Node.js compression app", diff --git a/src/download.html b/src/download.html index 22a6d68..7c377f0 100644 --- a/src/download.html +++ b/src/download.html @@ -5,46 +5,19 @@

Choose a File and Compression Type, then Upload

-
- - + - -
+ +
- - diff --git a/src/index.js b/src/index.js index 83fd659..acf99d7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,36 +1,42 @@ 'use strict'; -import http from 'http' -import fs from 'fs' -import zlib from 'zlib' -import formidable from 'formidable'; - +const http = require('http'); +const fs = require('fs'); +const zlib = require('zlib'); +const formidable = require('formidable'); const server = new http.Server(); -server.on('request', async (req, res) => { +server.on('request', async(req, res) => { if (req.url === '/upload') { const form = formidable({}); - let [fields, files] = await form.parse(req); - res.setHeader('Content-Type', 'text/plain'); + const [fields, files] = await form.parse(req); + res.setHeader('Content-Disposition', 'attachment; filename="text.txt"'); - let compressionStream - switch (fields.compressionType[0]) { + let compressionStream; + + switch (fields.select[0]) { case 'gzip': compressionStream = zlib.createGzip(); res.setHeader('Content-Encoding', 'gzip'); break; case 'deflate': - compressionStream = zlib.createDeflate() + compressionStream = zlib.createDeflate(); res.setHeader('Content-Encoding', 'deflate'); break; } - console.log('files.file[0].filepath', files.file[0].filepath); - console.log('compressionStream', compressionStream); const fileStream = fs.createReadStream(files.file[0].filepath); - fileStream.pipe(compressionStream).pipe(res); + fileStream + // eslint-disable-next-line + .on('error', (error) => console.log('fileStream ERROR', error)) + .pipe(compressionStream) + // eslint-disable-next-line + .on('error', (error) => console.log('compressionStream ERROR', error)) + .pipe(res) + // eslint-disable-next-line + .on('error', (error) => console.log('res ERROR', error)); } if (req.url === '/') { @@ -46,9 +52,11 @@ server.on('request', async (req, res) => { }); server.on('error', (error) => { - console.log('error', error) -}) + // eslint-disable-next-line + console.log('server error', error); +}); server.listen(3000, () => { + // eslint-disable-next-line console.log('server running...'); });