Created
June 21, 2012 01:04
-
-
Save ovaillancourt/2963261 to your computer and use it in GitHub Desktop.
Small file upload app - minimal demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Small upload form --> | |
<html><body> | |
<form action="/upload" | |
enctype="multipart/form-data" method="post"> | |
Please specify a file, or a set of files:<br> | |
<input type="file" name="myfile" size="40"> | |
</p> | |
<div> | |
<input type="submit" value="Send"> | |
</div> | |
</form> | |
</body></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express.createServer(); | |
var fs = require('fs'); | |
app.use(express.bodyParser({ | |
uploadDir : __dirname //That is mandatory. Tells express where to put the | |
//file after upload. | |
})); | |
app.use(app.router); | |
//That's where the upload lands. | |
app.post('/upload', function(req, res, next){ | |
res.send( "File with name: " + req.files.myfile.name + | |
"has been uploaded at path:" + req.files.myfile.path); | |
//Just remove the file to cleanup since it's just a small example. | |
fs.unlinkSync(req.files.myfile.path); | |
}); | |
//Just serve a small generic html file with a file upload form. | |
app.get('*', function(req, res, next){ | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment