Created
December 16, 2011 22:41
-
-
Save betobaz/1488361 to your computer and use it in GitHub Desktop.
node.js Upload file formidable
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
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, routes = require('./routes') | |
, stylus = require('stylus') | |
, util = require('util') | |
//, form = require('connect-form') | |
, formidable = require('formidable') | |
, sys = require('sys') | |
; | |
var app = module.exports = express.createServer( | |
); | |
// Configuration | |
function compile(str, path){ | |
return stylus(str) | |
//.import(__dirname + '/public/stylesheets/cruz-azul') | |
.set('filename', path) | |
.set('warn', true) | |
.set('compress', true); | |
}; | |
app.configure(function(){ | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(require('stylus').middleware({ src: __dirname + '/public', compile: compile })); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
//console.log('hola, esto trae sucursalprovider:', SucursalProvider); | |
// Routes | |
app.get('/upload-file', function(req, res){ | |
res.render('upload-file/index',{ | |
title: "Upload File" | |
}); | |
}); | |
app.post('/upload-file', function(req, res, next){ | |
var form = new formidable.IncomingForm(), | |
fields = []; | |
form | |
.on('error', function(err){ | |
res.writeHead(200, {'content-type':'text/plain'}); | |
res.end('error:\n\n'+util.inspect(err)); | |
}) | |
.on('field', function(field, value){ | |
console.log(field, value); | |
fields.push([field, value]); | |
}) | |
.on('end', function(){ | |
console.log('-> post done'); | |
res.writeHead('200', {'content-type':'text/plain'}); | |
res.end('received fields:\n\n'+util.inspect(fields)); | |
}); | |
console.log(req); | |
form.parse(req); | |
}); | |
app.listen(3000); | |
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crear el directorio bucket