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

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.

    • JM Mta

      Thanks you,,,,I really appreciate your help, but still I don’t know how do it, I’ve gone to the link you put and I read it,,,, but I don’t understand how built it the process, sorry for my sillyness…may you give me some clue PLEASE…!!!!!!

    • Paul Shan

      It seems they changed the doc. It was not this small before. Anyway, first tell me one thing. Your requirement is remove those passwords completely? Or you feel this is a security threat as the password is displayed in the app itself and someone may use it?
      If case 1: You need to configure your server, so that it doesn’t allow request from every machine, but only from the machines (ip addresses) you allow.
      If case 2: You create a file in your machine environment. i.e. /root/neo/credentials.json and put the username and password there. The application will require that file and set the username and password dynamically while calling neo4j. So whenever you are deploying you application in your production server, it will pick the password (which will be different then someone’s local password) from that server machine itself.

    • JM Mta

      I really appreciate again Paul…!!!, for all the information u’re guiding to me, this is the issue: I’m trying to do an app, where the user log on and off, I mean, I’ve changed the neo4j password default e.g: neo4j, neo4j for neo4j,N304j,….and on the app in the index.js, the user has to introduce a user/password but I don’t want appear on the code, the app will looking for dynamically the data given by user, if doesn’t exist app will not let log on the user, otherwise it let log on, I’m working in Windows OS, I beg your guideness PLEASE…!!!,

    • Paul Shan

      Oh.. then your case is very simple. You just want the user to provide id, pass. So use “readline” package to make the app ask questions and receive answers from the user via your command prompt. Save those in variables (you can use global variables in your case) and pass on to your neo4j-driver whenever needed.
      Let me know if I understood the problem wrong or you have more issues.

    • onewiseburdman

      Thats terrible advice – one could spoof an IP address and Mac address very easily. Use JWT do it the right way.

    • Paul Shan

      using jwt will not solve his problem here. He said, he doesn’t wanna expose credentials. His developers will easily get the token from the codebase.
      Fight spoofing, not methods.

  • Meiyanatham P

    I am building an app with Angular, Node, express and Neo4j. I just want to know how to do secured login and logout using neo4j driver?
    it would be better if i can implement with JWT based authentication. Please give some ideas to implement this.


    • Paul Shan

      JWT has the overhead of managing blacklisting and traditional session based system has the overhead of memory overflow.
      You can choose any depending on your scenarios. You can check passport.js once, very popular to be used with express for authentication.

    • Meiyanatham P

      Thanks bro. How to store these username and password in neo4j based on the user? any ideas?

    • Paul Shan

      You need to create a Node of type USER for each user, with username and password (try to use hash instead of actual password) in it.

  • Sucheta Shrivastava

    Hi Paul, I want to show only the output of Neo4j graph nodes and its tree diagram in my Angular and NodeJs app. Like in ELK (kibana) we have an iframe and by giving just the iframe link in our application we can show the graphs and visualizations. Anything of this sort available in Neo4j ? Or can be created ?