Setting up dGraph with NodeJS and Express?

I have Docker, the JS client and dgraph setup fine on a server instance per the guides.
I have also setup a sample node web app instance using Express (also by tutorial)

I tried to mesh the two together to see if i can get a RESTful request to query the database…

'use strict';

const express = require('express');
const dgraph = require("dgraph-js");
const grpc = require("grpc");

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.get('/sample', async function (req, res, next) {
  console.log('Accessing the secret section ...')
    const dgraphClientStub = newClientStub();
    const dgraphClient = newClient(dgraphClientStub);
    
    await setSchema(dgraphClient);
    dgraphClientStub.close();
    res.send('Entered\n');
})

// Create a client stub.
function newClientStub() {
    return new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
}

// Create a client.
function newClient(clientStub) {
    return new dgraph.DgraphClient(clientStub);
}

// Set schema.
async function setSchema(dgraphClient) {
    const schema = `
        name: string @index(exact) .
        age: int .
        married: bool .
        loc: geo .
        dob: datetime .
    `;
    const op = new dgraph.Operation();
    op.setSchema(schema);
    await dgraphClient.alter(op);
}

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

This didn’t work of course.

Running on http://0.0.0.0:8080
Accessing the secret section ...
(node:17) UnhandledPromiseRejectionWarning: Error: 14 UNAVAILABLE: failed to connect to all addresses
    at Object.exports.createStatusError (/usr/src/app/node_modules/grpc/src/common.js:91:15)
    at Object.onReceiveStatus (/usr/src/app/node_modules/grpc/src/client_interceptors.js:1204:28)
    at InterceptingListener._callNext (/usr/src/app/node_modules/grpc/src/client_interceptors.js:568:42)
    at InterceptingListener.onReceiveStatus (/usr/src/app/node_modules/grpc/src/client_interceptors.js:618:8)
    at callback (/usr/src/app/node_modules/grpc/src/client_interceptors.js:845:24)
(node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I would appreciate if someone could guide me in the right direction or if I’m way off already.

I’ve tested here and all is working. Maybe the addr isn’t exposed.
Instead of localhost:9080 try “DockerMachineIP:9080”

40

Thanks for the reply! That’s exactly right, setting the IP to the server instance IP fixed it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.