ExpressJS Multer File Persmission

var express = require('express');
var app = express();
var fs = require("fs");

var bodyParser = require('body-parser');
var multer  = require('multer');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/new/'}));

This is starting of file. It is always showing below message during startup the server

/usr/lib/node_modules/multer/node_modules/mkdirp/index.js:90 throw err0; ^ Error: EACCES, permission denied '/new' at Object.fs.mkdirSync (fs.js:654:18) at Function.sync (/usr/lib/node_modules/multer/node_modules/mkdirp/index.js:71:13) at new DiskStorage (/usr/lib/node_modules/multer/storage/disk.js:21:12) at module.exports (/usr/lib/node_modules/multer/storage/disk.js:65:10) at new Multer (/usr/lib/node_modules/multer/index.js:15:20) at multer (/usr/lib/node_modules/multer/index.js:75:12) at Object. (/var/www/node/server.js:10:9) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32)

Please let me if someone can help me to prevent this problem. Thank you so much.


multer is trying to create a destination folder /new in global node_modules location. Change your path to local destination. Try the following

app.use(multer({ dest: './new/'}));

if you are getting error with the above try

app.use(multer({ dest: __dirname +'/new/'}));

You have two isues:

1) You set global destination path to multer. And multer has not permission to create that path (and it is correct). I suspect you want to create 'new' directory in your project, if yes, then change your path to './new/'. When you fix this then we have second problem.

2) Multer module change API. It will not work:

app.use(multer({ dest: './new/'}));

unless you have an old version of multer (dont use old version it has security isues). Check docs page for how to use multer: link. You can check my answer to similar problem: link. In short:

configure destination path:

var upload = multer({ dest: './new' })

then in each route where you want to upload file

app.post('/profile', upload.single('fieldname'), function (req, res, next) {
//req.file has data of uploaded file
}
链接地址: http://www.djcxy.com/p/33026.html

上一篇: 无法导入用于摩卡和酶测试的Reactjs类

下一篇: ExpressJS Multer文件保护