I'm building an express api in front of a neo4j database. The whole project runs two services in docker:
- neo4j-database
- characters-api
I use an .env file to set all environment variables, hence the mapping in the docker-compose.yml.
Both are connected through a network called "neo4j-database-nw". Express (by default creation with the "express-generator") has the myapp/bin/www file as entrypoint. This is where I create my neo4j driver instance, which I also added to the export of the module, as the driver shall be available throughout the application.
When I create a controller for a route (e.g. /test) and and create a session in that module, which then runs a simple query, I get the following error: "Neo4jError: Client network socket disconnected before secure TLS connection was established".
In the myapp/bin/www file I create the driver with the following parameters:
const neo4jDriver = neo4j.driver(bolt://neo4j-database:7687
, neo4j.auth.basic(neo4j, test));
And I close the driver not earlier than with the terminus package during the "onSignal" event, which is, when the application receives one of the following signals "['SIGTERM', 'SIGINT']".
What am I missing to establish a connection with the database from my nodejs application?
myapp/controllers/test/testController.ts:
import Debug from 'debug';
import express from 'express';
import { neo4jDriver } from '../../bin/www';
const debug = Debug('neo4j:session');
const router = express.Router();
/* GET users listing. */
router.get('/', async (req, res, next) => {
const query = 'CREATE (c:Character) SET c.name = {name} RETURN c';
const params = {};
const session = neo4jDriver.session();
try {
const result = await session.run(query, params);
res.status(200).send(result);
} catch (error) {
debug(`Unable to execute query. ${error}`);
res.status(500).send(error);
} finally {
session.close();
}
});
export default router;
docker-compose.yml:
version: "3.7"
services:
characters-api:
image: characters-api:latest
container_name: characters-api
build: .
depends_on:
- neo4j-database
command: sh -c 'sh ./install-dependencies.sh && ./node_modules/.bin/tsc-watch --project ./tsconfig.json --skipLibCheck --noClear --onSuccess "npm run compile-and-run"'
environment:
NEO4J_API_CONTAINER_PORT: ${NEO4J_API_CONTAINER_PORT}
NEO4J_API_HOST_PORT: ${NEO4J_API_HOST_PORT}
NEO4J_USERNAME: ${NEO4J_USERNAME}
NEO4J_PASSWORD: ${NEO4J_PASSWORD}
NEO4J_DATABASE: ${NEO4J_DATABASE}
NEO4J_BOLT_PORT: ${NEO4J_BOLT_PORT}
NEO4J_HOSTNAME: ${NEO4J_HOSTNAME}
ports:
- ${NEO4J_API_HOST_PORT}:${NEO4J_API_CONTAINER_PORT}
volumes:
- .:/usr/src/app
networks:
- neo4j-database-nw
neo4j-database:
image: neo4j:4.0.0
container_name: neo4j-database
environment:
NEO4J_AUTH: ${NEO4J_AUTH}
ports:
- 7474:7474
- 7473:7473
- 7687:7687
volumes:
- ./neo4j-database/data:/data
- ./neo4j-database/logs:/logs
- ./neo4j-database/import:/var/lib/neo4j/import
- ./neo4j-database/plugins:/plugins
networks:
- neo4j-database-nw
networks:
neo4j-database-nw: