ang
ang

Reputation: 1581

Set Path for Mounted Network Drive in Node

I am trying to write to a mapped network drive in Node using the windows-network-drive module and the fs module.

networkDrive.mount('\\\\server', 'Z', 'username', 'password')
  .then(driveLetter => {

  let filePath;

  filePath = path.join(driveLetter + ":\\path\\to\\directory", "message.txt");

  fs.writeFile(filePath, "text", (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
  });
})
.catch(err => {
  console.log(err)
});

How can I get the connection and path to write to a remote location?

Do I need to pass in the drive letter? If so, how do I locate it?

(node:4796) UnhandledPromiseRejectionWarning:
ChildProcessError: Command failed: net use Z: "\server" /P:Yes /user:username password System error 67 has occurred.

The network name cannot be found.

net use Z: "\server" /P:Yes /user:username password (exited with error code 2)
at callback (C:\app\location\node_modules\child-process-promise\lib\index.js:33:27)
at ChildProcess.exithandler (child_process.js:279:5)
at ChildProcess.emit (events.js:159:13)
at maybeClose (internal/child_process.js:943:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
name: 'ChildProcessError',
code: 2,
childProcess:

{ ChildProcess: { [Function: ChildProcess] super_: [Function] },
fork: [Function],
_forkChild: [Function],
exec: [Function],
execFile: [Function],
spawn: [Function],
spawnSync: [Function: spawnSync],
execFileSync: [Function: execFileSync],
execSync: [Function: execSync] },
stdout: '',
stderr: 'System error 67 has occurred.\r\n\r\nThe network name cannot be found.\r\n\r\n' }

P.S. this code logs Z

networkDrive.mount('\\\\server\\path\\to\\directory', 'Z', 'mdadmin', 'Password1!')
  .then(function (driveLetter) {
    console.log(driveLetter);
    fs.writeFile('L_test.txt', 'list', (err) => {
      if (err) throw err
    })
});

Upvotes: 0

Views: 5922

Answers (2)

ang
ang

Reputation: 1581

To write from a REST Service hosted in IIS, you will need to properly set the permissions on the server.

  1. You will need to set the identity of the Application Pool of your site.

enter image description here

  1. You will need to grant write permissions to match that account or account group to the folder that you are trying to write to.

Note: If you map a folder to a network drive letter through the OS, it is only defined at the user account level.

enter image description here

  1. So, if you have mapped the folder location to a drive letter (in this case 'X:'), instead of writing to
fs.writeFile('X:/test.txt', 'text', (err) => {
  if (err) throw err
})

You must write to to full path

fs.writeFile('\\\\servername\\path\\to\\director\\test.txt', 'text', (err) => {
  if (err) throw err
})

Note: Backslashes need to be escaped, so the Windows file system will show something like \\servername\path\to\directory.

P.S. This answer includes recommendations from users l-bahr and Ctznkane525.

Upvotes: 1

L Bahr
L Bahr

Reputation: 2901

I am not sure what error you got, so here are a couple tips for when you are using windows-network-drive.

Escape Special Characters

Windows uses \ to separate directories. \ is a special character in a JavaScript string and must be escaped like this \\. e.g. C:\file.txt would be C:\\file.txt in a string.

Use POSIX Separation Character When You Can

Because of the added difficulty in reading a path with the escaped \, I would recommend using / instead. windows-network-drive should handle both just fine. e.g. C:\file.txt would be C:/file.txt in a string.

Example

I tried to make this match your example, but made a few changes so that it will work on any windows machine.

let networkDrive = require("windows-network-drive");

/**
 * https://github.com/larrybahr/windows-network-drive
 * Mount the local C: as Z:
 */
networkDrive.mount("\\\\localhost\\c$", "Z", undefined, undefined)
    .then(function (driveLetter)
    {
        const fs = require("fs");
        const path = require("path");
        let filePath;

        /**
         * This will create a file at "Z:\message.txt" with the contents of "text"
         * NOTE: Make sure to escape '\' (e.g. "\\" will translate to "\")
         */
        filePath = path.join(driveLetter + ":\\", "message.txt");
        fs.writeFile(filePath, "text", (err) =>
        {
            if (err) throw err;
            console.log('The file has been saved!');
        });
    });

Upvotes: 0

Related Questions