问题
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