how can I access the uploadDir attribute of express?

浪尽此生 提交于 2019-12-10 21:02:16

问题


In express app.js I define uploadDir = "./tmp", but how can I access it later?

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));

  app.use(express.bodyParser({uploadDir:'./tmp', keepExtensions: true})); // <--

  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

回答1:


Arguments to middleware like bodyParser go straight through to the middleware. All of the Express middleware is provided by Connect, so it doesn't even know anything about Express. The uploadDir is captured in the multipart closure of the multipart middleware. It is stored via the closure, and never stored anywhere else, or passed to Express, so the only way to access the original value is to access it as part of the options object initially passed in. There is no other way.

If you want that value to be accessible by reading from app(as you said in your comment), then you should set it on there yourself separately. That said, this method is a bit ugly and means that you have to set the value twice.

app.set('uploadDir', './tmp');


来源:https://stackoverflow.com/questions/15562273/how-can-i-access-the-uploaddir-attribute-of-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!