How to write a basic read write operation of neo4j with node.js in express apis?

As you have landed on this article, I assume you already know what neo4j or graph database is. This article describes how to expose express.js apis to do a very basic write and read operation. You can do it without using express also.

Things to be known

  • Neo4J uses cypher query language.
  • Neo4J provides very nice rest apis to interact without any intermediate driver.
  • Though it has a driver. You can use this driver also to make development simpler.
  • This article shows how to node neo4j using its http endpoints and the last paragraph shows an example to use the thingdom driver.

So scaffold your node project first. you can use npm init or you can directly clone or fork our github repo to start with.

Starting neo4j server

Hope you already know how to start a neo4j server. You need to install neo4j. The community edition is free. After installing start the software and it will run the database on port 7474 by default.

app.js basic setup

so we just exposed two http rest apis with express.js and both of them are empty at the moment.

Api authentication

Every single http rest call to neo4j apis need an authentication token in the header which is nothing but the base64 encryption of your username:password string. Below is an example to do that and also a function which will do the database operation for us.

Below is a function which makes the call to neo4j database with request module of node.js (you can also use node’s http.request for the same purpose)

create operation

read operation

Parameterized read write

npm plugins for neo4j

You can also use plugins to perform the calls in a more sophisticated way. Neo4j has it’s official node plugin. Below is an example how this plugin works

Still having issues?

If you still have issues understanding the above article, please leave a comment below and we will try to address that. In case you need help in your projects or understanding anything related to Programming; contact me, Paul Shan for assistance. Thank you for being a reader of VoidCanvas.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • JM Mta

    hello, I’m novell user in JS and neo4j, I’m triying to do a authentication login/user in nodejs and neo4j, I’ve read the official manual neo4j but isn’t complete I mean, the information is too basic (they… use var driver = neo4j.driver(“bolt://localhost”, neo4j.auth.basic(“neo4j”, “neo4j”)); and I don’t wanna show the standard password, how may I do this in other way..?), please help me.

    • Paul Shan

      Stop doing username/password based authentication and make neo4j to accept request from only a particular ip (your own server ip). Thus, neo will reject requests from others, but when it will receive request from your own application, it will respond.