简体   繁体   English

每个用户的express.js唯一目录

[英]express.js unique directory for each user

I have an express.js app , and I am using drywall in order to manage the user system. 我有一个express.js 应用程序 ,并且我正在使用drywall来管理用户系统。

When a user signs up, I want a directory to be generated for that user, and I want that user to be able to upload files to that directory and view those files through his or her account. 当用户注册时,我希望为该用户生成一个目录,并且希望该用户能够将文件上传到该目录并通过其帐户查看这些文件。

I am not entirely sure, but I think that most likely for the directory generation I will have to do that inside views/signup/index.js, and that the user can only upload files to his or her directory if logged in. 我并不完全确定,但是我认为最有可能在目录生成过程中在views / signup / index.js中执行该操作,并且用户只有登录后才能将文件上传到他或她的目录中。

However, I'm a bit stuck when it comes to saving and displaying the files. 但是,在保存和显示文件时我有些卡住。 I have little experience with server side code, so implementing actions such as accessing files is slightly beyond my scope. 我对服务器端代码的经验很少,因此实现访问文件之类的操作超出了我的范围。

Thanks in advance to those who help. 在此先感谢提供帮助的人。

So first you should create a folder for each user by using fs.mkdir : 因此,首先您应该使用fs.mkdir为每个用户创建一个文件夹:

http://nodejs.org/api/fs.html#fs_fs_mkdir_path_mode_callback http://nodejs.org/api/fs.html#fs_fs_mkdir_path_mode_callback

Let's say you want to create these folders into your app root / images: 假设您要将这些文件夹创建到应用程序的根目录/图像中:

Example: 例:

var fs = require('fs');
fs.mkdir(__dirname + '/images/' + userId, function(err) {
  if (err) {
    /* log err etc */
  } else { 
    /* folder creation succeeded */
  }
});

You should probably use the userId for the folder name (since it's easier than trying to strip out the bad characters from the username itself, and this will also work in the future if the user changes his username). 您可能应该使用userId作为文件夹名称(因为它比尝试从用户名本身中删除坏字符要容易得多,并且如果用户更改其用户名,将来也可以使用)。

The second thing you need to do is to allow the user to upload files (but only if he is logged in and into the right folder). 您需要做的第二件事是允许用户上传文件(但仅当用户登录并进入正确的文件夹时)。 It's better to not include the bodyParser middleware for all routes, but instead include the json && urlencoded middleware for all routes ( http://www.senchalabs.org/connect/json.html && http://www.senchalabs.org/connect/urlencoded.html ) and the multipart middleware only for the upload url ( http://www.senchalabs.org/connect/multipart.html && example: https://github.com/visionmedia/express/blob/master/examples/multipart/index.js ). 最好不要为所有路由都包含bodyParser中间件,而应为所有路由都包含json && urlencoded中间件( http://www.senchalabs.org/connect/json.html && http://www.senchalabs.org/ connect / urlencoded.html )和仅用于上载网址的multipart中间件( http://www.senchalabs.org/connect/multipart.html &&示例: https : //github.com/visionmedia/express/blob/master/ examples / multipart / index.js )。

An example: 一个例子:

app.post('/images', express.multipart({ uploadDir: '/tmp/uploads' }), function(req, res, next) {
  // at this point the file has been saved to the tmp path and we need to move
  // it to the user's folder
  fs.rename(req.files.image.path, __dirname + '/images/' + req.userId + '/' + req.files.image.name, function(err) {
    if (err) return next(err);

    res.send('Upload successful');
  });
});

Note: in the example above I've taken into consideration that req.userId is populated with the id of the user by an auth middleware. 注意:在上面的示例中,我已经考虑到auth中间件将req.userId填充为用户的ID。

Showing the images to the user if he has the rights to see them (the auth middleware should be applied for this path as well): 向用户显示图像(如果他有权查看图像)(身份验证中间件也应应用于此路径):

app.get('/images/:user/:file', function(req, res, next) {
  var filePath = __dirname + '/images/' + req.userId + '/' + req.params.file;

  fs.exists(filePath, function(exists) {
    if (!exists) { return res.status(404).send('Not Found'); }

    // didn't attach 'error' handler here, but you should do that with streams always
    fs.createReadStream(filePath).pipe(res);
  });
});

Note: in production you might want to use send instead, that example was just demoing the flow ( https://github.com/visionmedia/send ). 注意:在生产中,您可能想使用send代替,该示例只是演示流程( https://github.com/visionmedia/send )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM