'A function doesn't work when called by a http request but works when directly called on the script

I created a node.js function which play a sound file when called :

var audio = require('play-sound')(opts = {});
var file = '../Mario-Bros-Sms.mp3'

exports.bip = function (){
    return new Promise((resolve, reject) => {
        audio.play(file, function(err){
            if (err) reject(err)
        });
        resolve({ message: 'Notification envoyée' }); 
    });
}

This function works well when I call it directly on the script and run the script, but when I call it from a HTTP request, I get the right answer 'Notification envoyée' on my host but the file is not played on my server. Here is the controller corresponding :

const action = require('../functions/functions');

exports.bip = (req, res) => {
    action.bip()
    .then((answer) => res.status(200).json(answer))
    .catch(error => res.status(400).json({error}));
};

Here is the answer I get on postman : postman_answer

Could you understand why the file is not played on my server please?

Here is the code of the server.js file :

const http = require('http');
const app = require('./app');

const normalizePort = val => {
  const port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }
  if (port >= 0) {
    return port;
  }
  return false;
};
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

const errorHandler = error => {
  if (error.syscall !== 'listen') {
    throw error;
  }
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port: ' + port;
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges.');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use.');
      process.exit(1);
      break;
    default:
      throw error;
  }
};

const server = http.createServer(app);

server.on('error', errorHandler);
server.on('listening', () => {
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port ' + port;
  console.log('Listening on ' + bind);
});

server.listen(port);

And here is the code of the app.js file :

const express = require('express');
const printRoutes = require('./routes/new_order');

const app = express();

app.put((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization');
    res.setHeader('Access-Control-Allow-Methods', 'PUT');
    next();
});

app.use(express.json());

app.use('/new_order', printRoutes);

module.exports = app;


Solution 1:[1]

You need to indicate the full path to a music file:

const path = require('path')
const file = '../Mario-Bros-Sms.mp3'

exports.bip = function (){
    return new Promise((resolve, reject) => {
        audio.play(path.join(__dirname, file), function(err){
            if (err) reject(err)
        });
        resolve({ message: 'Notification envoyée' }); 
    });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anatoly