2016-04-01 58 views
1

在node-js中,我使用multer包來上傳圖像。壓縮我使用的圖像image-min「image-min」在節點js中的「multer」內不起作用

var multer = require('multer'); 
var imagemin = require('image-min'); 

app.use(multer({ 
dest : './public/images/profiles/uploads/', 
rename : function(fieldname, filename) { 
    return (filename.split(" ").join("")) + Date.now(); 
}, 
onFileUploadStart : function(file) { 
    console.log(file.originalname + ' is starting ...'); 
}, 
onFileUploadComplete : function(file) { 

    var imagemin = new Imagemin().src(file.path).use(compressionType); 
    imagemin.run(function(err, files) { 
     if (err) { 
      return next(err); 
     } 
     console.log('Files optimized successfully!'); 
    }); 
    console.log(file.fieldname + ' uploaded to ' + file.path); 
    done = true; 
} 
})); 

當我運行我的代碼拋出錯誤。

var imagemin = new Imagemin().src(file.path).use(compressionType);
ReferenceError: Imagemin is not defined

+0

變種Imagemin =要求( 'imagemin');頂部可能是?另外,您是否安裝了Imagemin? –

+0

我已經添加了上面的行並安裝了image-min – Shanthi

回答

0
var imagemin = require('image-min'); 

應該是:

var Imagemin = require('image-min'); 
+0

這個修正了錯誤。但現在它會拋出新的錯誤 var imagemin = new Imagemin()。src(file.path).use(compressionType); TypeError:對象#沒有方法'src' – Shanthi