0

I tried installing this package : https://github.com/FLYBYME/node-transmission in my local nodejs installation but I am getting following error while running example.js from the above github repository.

    Error: connect ECONNREFUSED 127.0.0.1:9091
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 9091

After a bit research I tried to run a server at port 9091 using this code(in a separate server.js file)

const http = require('http')  
const port = 9091

const requestHandler = (request, response) => {  
  console.log(request.url)
  response.end('Hello Node.js Server!')
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {  
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

After running a server on port 9091, I started getting this error with example.js :

SyntaxError: Unexpected token H in JSON at position 0
at JSON.parse (<anonymous>)
at IncomingMessage.onEnd (F:NodeJS\node-transmission-master\lib\transmission.js:453:33)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)

What must be causing this? I have no idea which step I have done wrong. That's why I described the whole process I followed.

I am very new to nodejs. Any help will be deeply appreciated.

MR-4O4
  • 311
  • 1
  • 2
  • 11

2 Answers2

1

The library is expecting a JSON formatted response and you are sending a simple text response. If you look through their source code you can see that their callServer function expects a stringified JSON but I can't see that in their docs.

You can change your code like so:

const http = require('http')  
const port = 9091

const requestHandler = (request, response) => {  
  console.log(request.url)

  // Format your response as a stringified JSON
  response.end(JSON.stringify({message: 'Hello Node.js Server!'}));
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {  
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})
Mike
  • 3,830
  • 2
  • 16
  • 23
  • Hi, I tried your code, now it shows this awkward error : `{ Error at IncomingMessage.onEnd (F:\NodeJS\node-transmission-master\lib\transmission.js:461:‌​29) at emitNone (events.js:91:20) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9) result: '{"message":"Hello Node.js Server!"}' }` – MR-4O4 Jan 15 '17 at 04:50
1

In order to run the example from the node-transmission package, you need first to install and start the transmission-daemon. The following steps are for Ubuntu:

  1. Login as root or change to root with su - (be always careful what you do/install as root)
  2. Install the transmission-daemon linux package: apt-get install transmission-daemon
  3. Edit the daemon configuration for either disabling authentication or setting up your username/password (there is no default password). You can disable authentication by editing the relative flag in the configuration file:

    • pico /etc/transmission-daemon/settings.json
    • Set the auth flag to false: rpc-authentication-required:false
    • Press Ctrl-X then Y and then Enter to save the change
  4. Start the daemon: start transmission-daemon

You should be able now to execute successfully the example.js and download its torrent link.

giorgos.nl
  • 2,704
  • 27
  • 36