Is there any way to access the Linux root ('/') directory through node? Right now, I have something like this:
multipart = require('connect-multiparty')
app.use(multipart({
uploadDir: config.tmp
}));
...
var file = req.files.file;
fs.renameSync(file.path, ".", function(err) {
if(err) console.error(err.stack);
})
but the problem is that file.path is refering to a folder inside the Linux root rather than the project's root.
The most literal answer to your question
Is there any way to access the Linux root ('/') directory through node?
Is yes, by using /
. Node.js shouldn't be giving any special treatment to it. Root is a directory just like any other.
On to your code...
fs.renameSync
takes a source first and destination second. You are renaming a file to .
, which represents the current working directory. I'm not even sure if you can rename something to .
. I would extract the filename from the path, then set the destination to the root directory plus that file name.
How to access the root directory, which as you said, is /
, well, use /
.
By the way, why are you using renameSync
with a callback and nothing after it? According to the documentation, this is not valid. It's either async with a callback, or sync without a callback. So your callback is probably not firing.
var file = req.files.file;
fs.rename(file.path, '/' + path.basename(file.path), function(err) {
if(err) console.error(err.stack);
});
By the way, I have to advocate strongly against an application writing files to the Linux root directory, for a number of reasons:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.