Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/f77a8411a7139090f66c to your computer and use it in GitHub Desktop.
Save kjunichi/f77a8411a7139090f66c to your computer and use it in GitHub Desktop.
Atom ShellでKoaを動かす

Atom ShellでKoaを動かす

マルチパートなアップロードをやってみる

koajs/examples/ multipart

mkdir testKoa

main.jsで--harmony

app.on('ready', function() {
  app.commandLine.appendSwitch('js-flags', '--harmony');

package.json

{
  "name": "my-test-ecma6",
  "productName": "Atom Shell Default App",
  "version": "0.1.0",
  "main": "main.js",
  "devDependencies": {
    "koa":"*",
    "co-fs":"*",
    "co-busboy":"*",
    "save-to":"*"
  }
}

index.html

<html>
<body>
<form action method="POST">
<input type="">
</form>
<script>
/**
* Multipart example downloading all the files to disk using co-busboy.
* If all you want is to download the files to a temporary folder,
* just use https://github.com/cojs/multipart instead of copying this code
* as it handles file descriptor limits whereas this does not.
*/

var os = require('os');
var path = require('path');
var koa = require('koa');
var fs = require('co-fs');
var parse = require('co-busboy');
var saveTo = require('save-to');

var app = module.exports = koa();

app.use(function *(){
  // parse the multipart body
  var parts = parse(this, {
    autoFields: true // saves the fields to parts.field(s)
  });

  // create a temporary folder to store files
  var tmpdir = path.join(os.tmpdir(), uid());

  // make the temporary directory
  yield fs.mkdir(tmpdir);

  // list of all the files
  var files = [];
  var file;

  // yield each part as a stream
  var part;
  while (part = yield parts) {
    // filename for this part
    files.push(file = path.join(tmpdir, part.filename));
    // save the file
    yield saveTo(part, file);
  }

  // return all the filenames as an array
  // after all the files have finished downloading
  this.body = files;
})

if (!module.parent) app.listen(3000);

function uid() {
  return Math.random().toString(36).slice(2);
}
</script>
</body>
</html>

動かすには

atom testKoa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment