Search code examples
node.jsdatabasesocketsorientdborientjs

Connecting to OrientDB in NodeJS using OrientJS module -- "Socket Closed" Error


I'm developing an application in NodeJS (v8.11.3) using OrientJS (v2.2.10) that will connect to a locally-hosted OrientDB (v3.0.6) database and query it. However, when I attempt to run my program, I receive a "Socket Closed" error every time.

I am able to connect to the database and query it through both OrientDB's console and through the web-interface, so I know the database is accessible at the address http://localhost:2480. If I run "netstat -a" in the Command Prompt, I can see that port 2480 is Listening for a TCP connection.

This is the code I am currently running:

//Import OrientJS driver for OrientDB
var OrientJs = require('orientjs');

//Connect to OrientDB server
var server = OrientJs({
    host: "localhost",
    port: "2480",
    username: "root",
    password: "root"
});

//Connect to 'demodb'
var db = server.use({
    name: 'demodb',
    username: 'root',
    password: 'root'
});

console.log("Connected to database")

//Select all entries in 'Castles' table and print to console
db.select().from('Castles').all()
.then(function(result) {
  console.log(result);
});

//Close connection to database
db.close();

The error I receive is:

Unhandled rejection OrientDB.ConnectionError [0]: Socket Closed
    at Connection.<anonymous> (C:\Program Files\nodejs\apollo_server\node_modules\orientjs\lib\transport\binary\connection.js:277:16)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

I'm not sure what to make of this error, as the database appears to be running fine. I was not able to find any useful information through googling this error; googling ""orientjs" "socket closed"" returns only 1 result, so I'm at a bit of a loss as to how to tackle this problem.

Any insight is greatly appreciated!


Solution

  • After further investigation, I've found the source of the problem.

    As it turns out, OrientDB hosts its service on two ports: 2480 (HTTP) and 2424 (Binary). Connecting to OrientDB via the OrientJS module requires using port 2424 rather than 2480. This solved my problem.