Node.js create folder or use existing

后端 未结 14 1435
暖寄归人
暖寄归人 2020-12-04 06:43

I already have read the documentation of Node.js and, unless if I missed something, it does not tell what the parameters contain in certain operations, in particular fs.mkdi

14条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 07:40

    The node.js docs for fs.mkdir basically defer to the Linux man page for mkdir(2). That indicates that EEXIST will also be indicated if the path exists but isn't a directory which creates an awkward corner case if you go this route.

    You may be better off calling fs.stat which will tell you whether the path exists and if it's a directory in a single call. For (what I'm assuming is) the normal case where the directory already exists, it's only a single filesystem hit.

    These fs module methods are thin wrappers around the native C APIs so you've got to check the man pages referenced in the node.js docs for the details.

提交回复
热议问题