Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ To pass the task you also need to implement a server that:
- use Streams
- use `zlib` module
- write server code in `createServer.js` file (it is used to test your app)
- respond with 404 status code if trying access a non-existent endpoint
- respond with 404 status code if trying access a non-existent endpoint-
- respond with 400 status code if trying send a GET request to `/compress` endpoint
- respond with 400 status code if the form is invalid
- respond with 400 status code if trying to compress a file with an unsupported compression type
- respond with 400 status code if the form is invalid-
- respond with 400 status code if trying to compress a file with an unsupported compression type-
- respond with 200 status code and compressed file if the form is valid

**Read [the guideline](https://github.com/mate-academy/js_task-guideline/blob/master/README.md) before start**
69 changes: 67 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
/* eslint-disable no-console */
'use strict';

const http = require('http');
const zlib = require('zlib');
const fs = require('fs');
const path = require('path');
const formidable = require('formidable');
const { pipeline } = require('stream');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const urlNormalized = new URL(req.url, `http://${req.headers.host}`);
const form = new formidable.IncomingForm();
const compressors = {
gzip: zlib.createGzip(),
deflate: zlib.createDeflate(),
br: zlib.createBrotliCompress(),
};

if (urlNormalized.pathname === '/compress' && req.method === 'POST') {
form.parse(req, (error, { compressionType }, { file }) => {
if (error || !compressionType || !file) {
res.writeHead(400, { 'Content-type': 'text/plain' });

return res.end('Invalid form data');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 🚀 to improve readability and maintainability you should create variable to store all possible messages

}

if (!Object.keys(compressors).includes(compressionType[0])) {
res.writeHead(400, { 'Content-Type': 'text/plain' });

return res.end('Unsupported compression type');
}

const fStream = fs.createReadStream(file[0].filepath);

res.writeHead(200, {
'Content-Disposition': `attachment; filename=${file[0].originalFilename}.${compressionType}`,
});

pipeline(fStream, compressors[compressionType[0]], res, (err) => {
if (err) {
res.statusCode = 400;
res.end(error.message);
}
});
});

return;
}

if (urlNormalized.pathname === '/compress' && req.method === 'GET') {
res.writeHead(400, { 'Content-Type': 'text/plain' });

return res.end('only POST method is not allowed');
}

if (urlNormalized.pathname === '/') {
const html = fs.createReadStream(path.resolve('src', 'index.html'));

res.setHeader('Content-Type', 'text/html');
html.pipe(res);

return;
}

res.statusCode = 404;

res.end('Bad request');
});
}

module.exports = {
Expand Down
22 changes: 22 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<form action="/compress" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<select name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button>Submit</button>
</form>
</body>

</html>
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
/* Don't change code below */

'use strict';

Expand Down
Loading