mirror of
https://github.com/tomasvarg/testground-web-devel.git
synced 2026-03-01 08:28:49 +00:00
Initial commit - upload-serve-files, download-file-request, echo-request
This commit is contained in:
commit
d3f5313362
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
package-lock.json
|
||||
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Testground Web Devel
|
||||
|
||||
Various web development related tests and examples.
|
||||
36
node/download-file-request/index.js
Normal file
36
node/download-file-request/index.js
Normal file
@ -0,0 +1,36 @@
|
||||
var http = require('http');
|
||||
var qs = require('querystring');
|
||||
|
||||
var body;
|
||||
var mime = 'application/zip';
|
||||
var port = 3000;
|
||||
|
||||
var server = http.createServer(function (req, res) {
|
||||
if (req.method !== 'POST') {
|
||||
res.writeHead(200);
|
||||
res.end('Only POST method supported so far!');
|
||||
};
|
||||
req.on('data', function (data) {
|
||||
body += data;
|
||||
|
||||
if (body.length > 1e6)
|
||||
req.connection.destroy();
|
||||
});
|
||||
req.on('end', function () {
|
||||
var post = qs.parse(body);
|
||||
|
||||
if (!post.file) {
|
||||
res.writeHead(200);
|
||||
res.end('No file provided!');
|
||||
}
|
||||
|
||||
var content = 'data:' + mime + ';base64,' + post.file;
|
||||
|
||||
res.writeHead(200, { 'Content-Type': mime });
|
||||
res.end(content, 'utf-8');
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, function () {
|
||||
console.log('File resender listening on port ' + port);
|
||||
});
|
||||
15
node/download-file-request/package.json
Normal file
15
node/download-file-request/package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "download-file-request",
|
||||
"version": "1.0.0",
|
||||
"description": "Offers download response for 'file' field post request",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"serve": "node index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": " <tomas.varga.cz@gmail.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {}
|
||||
}
|
||||
33
node/echo-request/echo-request.js
Normal file
33
node/echo-request/echo-request.js
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* A simple service just echoes received data
|
||||
*
|
||||
* usage: node echorequest.js [port=3001]
|
||||
*/
|
||||
|
||||
const http = require('http');
|
||||
|
||||
const hostname = '0.0.0.0';
|
||||
const port = process.argv[2] || 3001;
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
console.log(`\n${req.method} ${req.url}`);
|
||||
console.log('HEADERS:', req.headers);
|
||||
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
|
||||
var data = '';
|
||||
|
||||
req.on('data', function(chunk) {
|
||||
data += chunk
|
||||
});
|
||||
|
||||
req.on('end', function() {
|
||||
console.log('BODY: ' + data);
|
||||
res.end(data + "\n");
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, hostname, function () {
|
||||
console.log(`Server running at http://localhost:${port}/`);
|
||||
});
|
||||
8
node/echo-request/send-request-curl.sh
Executable file
8
node/echo-request/send-request-curl.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
port=${1:-3001}
|
||||
|
||||
curl -d 'par1=value1' \
|
||||
-d 'par2=value2' \
|
||||
-d 'par3=value3&par4=value4' \
|
||||
-XPOST "localhost:$port"
|
||||
2
node/upload-serve-files-koa_v1/.gitignore
vendored
Normal file
2
node/upload-serve-files-koa_v1/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
files/*
|
||||
98
node/upload-serve-files-koa_v1/index.js
Normal file
98
node/upload-serve-files-koa_v1/index.js
Normal file
@ -0,0 +1,98 @@
|
||||
var app = require('koa')();
|
||||
var mount = require('koa-mount');
|
||||
var serve = require('koa-static');
|
||||
var sendfile = require('koa-sendfile');
|
||||
var parse = require('co-busboy');
|
||||
var fs = require('fs');
|
||||
|
||||
var ROOT_DIR = __dirname + '/';
|
||||
var FILES_DIR = ROOT_DIR + 'files/';
|
||||
|
||||
app.use(function *showRequest(next) {
|
||||
console.log('path:', this.path, 'method:', this.method, '-----------------------');
|
||||
console.log('querystring:', this.request.querystring, 'query:', this.request.query);
|
||||
console.log('urlencoded:', this.request.is('urlencoded'), 'multipart:', this.request.is('multipart'));
|
||||
console.log('request:', this.request);
|
||||
yield next;
|
||||
});
|
||||
|
||||
app.use(serve(__dirname + '/public'));
|
||||
|
||||
app.use(function *getMultipartRequest(next) {
|
||||
if (!this.request.is('multipart'))
|
||||
return yield next;
|
||||
|
||||
var files = [];
|
||||
var parts = parse(this);
|
||||
var part;
|
||||
|
||||
while (part = yield parts) {
|
||||
if (!part.pipe) {
|
||||
console.log('fields:', part);
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
yield saveFile(part, FILES_DIR).then(function (resp) {
|
||||
files.push(resp);
|
||||
}, function (error) {
|
||||
files.push(error);
|
||||
});
|
||||
*/
|
||||
files.push(yield saveFile(part, FILES_DIR));
|
||||
}
|
||||
|
||||
this.body = files;
|
||||
});
|
||||
|
||||
app.use(mount('/download', function *sendFileHandler(next) {
|
||||
yield next;
|
||||
|
||||
var fname = this.request.query.file || 'archive.zip';
|
||||
|
||||
if (!(yield fileExists(FILES_DIR + fname)))
|
||||
return this.body = { message: 'File not found.', file: fname };
|
||||
|
||||
var stats = yield sendfile(this, FILES_DIR + fname);
|
||||
//console.log('sendfile response:', this.response);
|
||||
|
||||
if (!this.status)
|
||||
this.throw(404);
|
||||
}));
|
||||
|
||||
function saveFile(stream, destDir) {
|
||||
//return new Promise(function(resolve, reject) {
|
||||
return function(done) {
|
||||
var writer = fs.createWriteStream(destDir + stream.filename);
|
||||
var resp = { file: stream.filename };
|
||||
|
||||
stream.on('error', finish);
|
||||
writer.on('error', finish);
|
||||
|
||||
writer.on('close', function () {
|
||||
finish(null, { bytesWritten: writer.bytesWritten });
|
||||
});
|
||||
|
||||
stream.pipe(writer);
|
||||
|
||||
function finish(error, args) {
|
||||
resp.message = error ? 'saving ' + resp.file + ' failed:' + error
|
||||
: 'saved ' + resp.file + ' (' + args.bytesWritten + ' bytes)';
|
||||
//error ? reject(resp) : resolve(resp);
|
||||
done(null, resp);
|
||||
}
|
||||
//});
|
||||
}
|
||||
}
|
||||
|
||||
function fileExists(path) {
|
||||
return function(done) {
|
||||
fs.stat(path, function (err, res) {
|
||||
done(null, !err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
console.log('listening on port 3000');
|
||||
20
node/upload-serve-files-koa_v1/package.json
Normal file
20
node/upload-serve-files-koa_v1/package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "upload-serve-files-koa_v1",
|
||||
"version": "1.0.0",
|
||||
"description": "Testing point for downloads & uploads",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"serve": "node index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": " <tomas.varga.cz@gmail.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"co-busboy": "^1.3.1",
|
||||
"koa": "^1.2.0",
|
||||
"koa-mount": "^1.3.0",
|
||||
"koa-sendfile": "^2.0.0",
|
||||
"koa-static": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {}
|
||||
}
|
||||
15
node/upload-serve-files-koa_v1/public/404.html
Normal file
15
node/upload-serve-files-koa_v1/public/404.html
Normal file
@ -0,0 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Not Found</title>
|
||||
<style>
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px Helvetica, Arial;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Sorry! Can't find that.</h1>
|
||||
<p>The page you requested cannot be found.</p>
|
||||
</body>
|
||||
</html>
|
||||
20
node/upload-serve-files-koa_v1/public/index.html
Normal file
20
node/upload-serve-files-koa_v1/public/index.html
Normal file
@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Upload</title>
|
||||
<style>
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px Helvetica, Arial;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>File Upload</h1>
|
||||
<p>Try uploading multiple files at a time.</p>
|
||||
<form action="/" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="file" multiple>
|
||||
<input type="submit" value="Upload">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user