Skip to content

Instantly share code, notes, and snippets.

@alessioalex
Created March 29, 2011 19:56
Show Gist options
  • Save alessioalex/893110 to your computer and use it in GitHub Desktop.
Save alessioalex/893110 to your computer and use it in GitHub Desktop.
From Pedro Teixeira's http://nodetuts.com/tutorials/12-file-uploads-using-nodejs-and-express.html but with connect-form instead of multipart-js
var express = require('express'),
form = require('connect-form'),
fs = require('fs'),
util = require('util');
var app = express.createServer(
form({keepExtensions: true})
);
// switch between development and production like this:
// NODE_ENV=development node app.js
// OR
// NODE_ENV=production node app.js
// this always executes, no matter of production or dev environment
app.configure(function(){
// this logs in a Apache style
// app.use(express.logger());
app.use(express.bodyParser());
// this middleware will override our method
// with what we passed into the hidden variable _method
app.use(express.methodOverride());
// methodOverride must be after the bodyParser
app.use(express.static(__dirname + '/static'));
});
app.configure('development', function() {
app.use(express.logger());
// this is the error handler, uncomment #1 to see it in action
app.use(express.errorHandler({
dumpExceptions: true,
showStack : true
}))
});
app.configure('production', function() {
// this is the error handler for the production env
app.use(express.errorHandler({
dumpExceptions: false,
showStack: false
}));
});
app.set('views', __dirname + '/views');
app.set('view engine', 'jade')
// this is by default ->
// app.set('view options', {layout: true});
// if you don't want to have a layout though:
// app.set('view options', {layout: false});
app.get('/', function(req, res) {
// #1
// throw new Error('this is just my custom error');
// res.send('some text');
res.render('root');
});
var products = require('./products');
var photos = require('./photos');
app.get('/products', function(req, res) {
res.render('products/index', {locals: {
products: products.all
}});
});
app.get('/products/new', function(req, res){
res.render('products/new', {locals: {
product: req.body && req.body.product || products.new
}});
});
app.post('/products', function(req, res) {
var id = products.insert(req.body.product);
console.log(products.find(id));
res.redirect('/products/' + id);
});
app.get('/products/:id', function(req, res) {
var product = products.find(req.params.id);
if(product != null) {
res.render('products/show', {locals: {
product: product
}});
} else {
res.send('404 - Product not found!')
}
});
app.get('/products/:id/edit', function(req, res) {
var product = products.find(req.params.id);
if(product != null) {
photos.list(function(err, photo_list) {
if(err) {
throw err;
} else {
res.render('products/edit', {locals: {
product: product,
photos: photo_list
}});
}
});
} else {
res.send('404 - Product not found!')
}
});
app.put('/products/:id', function(req,res) {
var id = req.params.id;
products.set(id, req.body.product);
// req.body.product comes from bodyParser
res.redirect('/products/' + id);
});
/* Photos */
app.get('/photos', function(req, res) {
photos.list(function(err, photo_list) {
res.render('photos/index', {locals : {
photos: photo_list
}});
});
});
app.get('/photos/new', function(req, res) {
res.render('photos/new');
});
app.post('/photos', function(req, res) {
req.form.complete(function(err, fields, files) {
if(err) {
next(err);
} else {
ins = fs.createReadStream(files.photo.path);
ous = fs.createWriteStream(__dirname + '/static/uploads/photos/' + files.photo.filename);
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
res.redirect('/photos');
}
});
//console.log('\nUploaded %s to %s', files.photo.filename, files.photo.path);
//res.send('Uploaded ' + files.photo.filename + ' to ' + files.photo.path);
}
});
});
app.listen(4000);
console.log('Server listening on port 4000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment