Skip to content

Instantly share code, notes, and snippets.

@gildean
Created April 20, 2013 11:52
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 gildean/5425752 to your computer and use it in GitHub Desktop.
Save gildean/5425752 to your computer and use it in GitHub Desktop.
Express.js file-upload with error-handling.
!!!
head
title express-uploader
body
h1 express-uploader
p
a(href="/uploads") Uploaded files
p
form(action="/save", method="post", enctype="multipart/form-data")
input(type="file", name="upload")
input(type="submit", value="upload")
var express = require('express'),
app = express(),
server = require('http').createServer(app).listen(9999),
publicPath = __dirname + '/public';
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.favicon());
app.use(express.bodyParser({keepExtensions: true, uploadDir: publicPath + '/uploads'}));
app.post('/save', saveFile);
app.get('/', mainView);
app.use(express.directory(publicPath));
app.use(express.static(publicPath));
app.use(function (req, res, next) {
next('Not found');
});
app.use(function (err, req, res, next) {
res.send((err.status || 500), err.message);
});
function saveFile(req, res, next) {
res.redirect('/');
console.log(req.files);
}
function mainView(req, res, next) {
res.render('main');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment