I try to upload a png to my node.js server, but every time req.files.avatar
is called, an error appears and says TypeError: Cannot read property 'avatar' of undefined
.
The problem was that I haven't write enctype="multipart/form-data"
in the form. That's why req.files was undefined.
Also I think you have to include your bodyParser before any instructions to use app.router or static middleware. After some trial and error this is the order that works for me.
app.configure(function(){
app.set('port', process.env.port || 3000);
app.set('views', __dirname + '/app/server/views');
app.set('view engine', 'jade');
app.locals.pretty = true;
app.use(express.favicon());
app.use(express.bodyParser( { keepExtensions: true, uploadDir: __dirname + '/app/uploads' } ));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'super-duper-secret-secret' }));
app.use(app.router);
app.use(require('stylus').middleware({ src: __dirname + '/app/public' }));
app.use(express.static(__dirname + '/app/public'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With