Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions To Read Windows File Across Network in NodeJS

I'm trying to read a text file using NodeJS on a Mac. The file is on a Windows computer which is on the same network.

var fs = require('fs');
var file = "smb://someserver/stuff/names.txt";

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

When I run this code, I get:

> Error: ENOENT, open 'smb://someserver/stuff/names.txt'

I probably need to authenticate access to the file, but I haven't found any docs or modules that deal with this scenario. Is it possible to read/write files like this?

Update (10/22)

I've tried using UNC paths (ie \\someserver\stuff\names.txt) but I believe that is only part of the solution. Somehow I need to supply username/password/domain because this file requires special permission. Is it possible to supply permissions somehow? For example:

var fs = require('fs');
var file = '\\someserver\stuff\names.txt';
var credentials = { username:'mydomain/jsmith', password:'password' }; ???

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});
like image 254
bendytree Avatar asked Oct 03 '13 18:10

bendytree


1 Answers

You should use UNC paths if you are trying to access resources from other network drives.

var fs = require('fs');
var file = "\\someserver\stuff\names.txt";

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});
like image 169
Sriharsha Avatar answered Nov 14 '22 23:11

Sriharsha