initial dddd

This commit is contained in:
Artur Tamborski 2019-06-14 19:23:04 +02:00
commit 0a13b45cdd
2 changed files with 61 additions and 0 deletions

11
zad1.js Normal file
View File

@ -0,0 +1,11 @@
var http = require('http')
var path = require('path')
var fs = require('fs')
http.createServer((req, res) => {
res.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream(path.resolve(__dirname, 'index.html')).pipe(res);
}).listen(49376);
console.log('running...');
console.log('http://localhost:49376/');

50
zad2.js Normal file
View File

@ -0,0 +1,50 @@
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const app = express();
const PORT = 49376;
const UPLOAD_DIRECTORY = './uploads';
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => { cb(null, UPLOAD_DIRECTORY); },
filename: (req, file, cb) => { cb(null, file.originalname); }
})
}).single('file');
app.get('/', (req, res) => {
res.type('html');
fs.readdir(UPLOAD_DIRECTORY, (err, items) => {
var up = '';
for (var i = 0; i < items.length; i++) {
up += `<li><a href="/${items[i]}">${items[i]}</a></li>`;
}
up = up || 'There are no uploaded files...';
res.end(`
<h3>Upload form</h3>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="send" name="submit">
</form>
<h3>Files to download</h3>
<ul>${up}</ul>
`);
});
});
app.post('/', (req, res) => {
upload(req, res, (err) => {
if (err) { res.end('Error!'); }
else { res.redirect('/'); }
});
});
app.get('/:file(*)', (req, res) => {
res.download(UPLOAD_DIRECTORY + '/' + req.params.file, req.params.file);
});
app.listen(PORT, () => {
console.log(`Listening at http://localhost:${PORT}/`);
});