Tuesday, October 3, 2023

Connect to Samba server from nodejs app

 


const SMB2 = require('smb2');


// Replace these with your NAS credentials and share path

const username = 'your_username';

const password = 'your_password';

const sharePath = '\\\\nas_ip_or_hostname\\share_name';


const smbClient = new SMB2({

  share: sharePath,

  domain: '', // Leave empty or specify your domain if needed

  username,

  password,

});


smbClient.readFile('file_on_nas.txt', (err, fileContents) => {

  if (err) {

    console.error('Error reading file:', err);

  } else {

    console.log('File contents:', fileContents.toString());

  }


  // Close the connection when done

  smbClient.close();

});


No comments:

Post a Comment