Created
April 20, 2013 11:52
-
-
Save gildean/5425752 to your computer and use it in GitHub Desktop.
Express.js file-upload with error-handling.
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
!!! | |
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") |
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'), | |
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