diff --git a/examples/upload/index.js b/examples/upload/index.js new file mode 100644 index 0000000..7f822ea --- /dev/null +++ b/examples/upload/index.js @@ -0,0 +1,51 @@ + +/** + * Module dependencies. + */ + +var logger = require('koa-logger'); +var serve = require('koa-static'); +var parse = require('co-busboy'); +var koa = require('../..'); +var fs = require('fs'); +var app = koa(); + +// log requests + +app.use(logger()); + +// custom 404 + +app.use(function *(next){ + yield next; + if (this.body || !this.idempotent) return; + this.redirect('/404.html'); +}); + +// serve files from ./public + +app.use(serve(__dirname + '/public')); + +// handle uploads + +app.use(function *(next){ + // ignore non-POSTs + if ('POST' != this.method) return yield next; + + // multipart upload + var parser = parse(this); + var part; + + while (part = yield parser.part()) { + var stream = fs.createWriteStream('/tmp/' + Math.random()); + part.pipe(stream); + console.log('uploading %s -> %s', part.filename, stream.path); + } + + this.redirect('/'); +}); + +// listen + +app.listen(3000); +console.log('listening on port 3000'); \ No newline at end of file diff --git a/examples/upload/public/404.html b/examples/upload/public/404.html new file mode 100644 index 0000000..eb559a4 --- /dev/null +++ b/examples/upload/public/404.html @@ -0,0 +1,15 @@ + + + Not Found + + + +

Sorry! Can't find that.

+

The page you requested cannot be found.

+ + \ No newline at end of file diff --git a/examples/upload/public/index.html b/examples/upload/public/index.html new file mode 100644 index 0000000..8882344 --- /dev/null +++ b/examples/upload/public/index.html @@ -0,0 +1,20 @@ + + + + Upload + + + +

File Upload

+

Try uploading multiple files at a time.

+
+ + +
+ + \ No newline at end of file diff --git a/package.json b/package.json index e7ba904..3771815 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,10 @@ "supertest": "~0.8.1", "co-fs": "~1.1", "co-views": "~0.1.0", - "ejs": "~0.8.4" + "ejs": "~0.8.4", + "koa-logger": "~1.0.1", + "koa-static": "~1.2.0", + "co-busboy": "git://github.com/cojs/busboy" }, "engines": { "node": "> 0.11.4"