var http = require( 'http' ),
multipartser = require( 'multipartser' );
http.createServer( function ( req, res ) {
var contentType = req.headers && req.headers[ 'Content-Type' ];
if ( ! contentType ) {
res.writeHead( 400 );
res.end();
return;
}
var contentTypeParts = contentType.split( ';' );
if ( contentTypeParts.length != 2 ) {
res.writeHead( 400 );
res.end();
return;
}
contentType = contentTypeParts[ 0 ];
if ( contentType != 'multipart/form-data' ) {
res.writeHead( 415 );
res.end();
return;
}
var boundary = contentTypeParts[ 1 ];
boundary = boundary.trim().split( '=' );
if ( boundary.length != 2 ) {
res.writeHead( 400 );
res.end();
return;
}
boundary = boundary[ 1 ];
var parser = multipartser();
parser.boundary( boundary );
parser.on( 'part', function ( part ) {
if ( part.type == 'file' ) {
console.log( 'type', part.type );
console.log( 'name', part.name );
console.log( 'filename', part.filename );
console.log( 'contents', part.contents );
console.log( 'contentType', part.contentType );
} else if ( part.type == 'field' ) {
console.log( 'type', part.type );
console.log( 'name', part.name );
console.log( 'value', part.value );
}
});
parser.on( 'end', function () {
console.log( 'completed parsing' );
});
parser.on( 'error', function ( error ) {
console.error( error );
});
req.setEncoding( 'utf8' );
req.on( 'data', parser.data );
req.on( 'end', parser.end );
}).listen( 1337, '127.0.0.1' );