Using Neo4j with Express NodeJs

Posted By : Prabjot Singh | 25-Mar-2015

Here I am building simple app using database Neo4j. I will perform basic operation (CRUD) on Neo4j in my application. So before starting, you should have basic knowledge of Neo4j. Neo4j is graph database where data is stored in the form of entities and relationship. For more details,you can find at Introduction to Neo4j

So there are four popular NodeJS modules for connecting Neo4j with our application

  • Node-Neo4j (Thingdom)

  • Node-Neo4j (Philipkueng)

  • Neo4j-js

  • Seraph

They all support cypher end point. Apart from that,only philipkueng support transactional api. If I ask about labels of node,then only supported by philipkueng and seraph. There I am ended with philipkueng module.

First of all, you should have familiar with basics of express nodejs.if you haven't ,don't worry about

that,you can find my blog My first application with express NodeJS. Before move to next step, you have to install node-neo4j module


Download and Installing Node-Neo4j Module

Now you know better how to install node-neo4j module. You will install it by command “npm install node-neo4j” and after installing you will see a directory of node-neo4j inside node_modules package.

It will provide us library to connecting with neo4j.

Changes in Server.js

Compare to my previous blog,there are lot of changes in Server.js. We have installed node-neo4j module. Now,time to using in our program. Copy below code in your Server.js file


 var express=require('express');
var app=express();
var neo4j = require('node-neo4j'); // there we are importing dependencies
var db = new neo4j(“http://localhost:7474/”) // there you will provide your neo4j host url
 require('./router/main')(app,db);  // there we have to pass our database reference in main.js,and rest of lines are same as my previous one
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
var server=app.listen(3000,function(){
console.log("Express is running on port 3000");

Changes in Main.js

We have to do some changes in main.js. In my preveous blog,we were rendering simple content but now we will do crud operation with neo4j ,we will render database information. Please copy below code in main.js


{'/user', function (req, res) {
        		name: 'Prabjot',
        		sex: 'male'
    		},function(err, node){
       		 if(err) throw err;
       			 // Output node properties.
       			 // Output node id.

    app.get('/user', function (req, res) {
        db.readNode(398, function(err, node){
                console.log("err is"+err);
                throw err;
            // Output node properties.
            res.render('index.html',{node:node}); // so there is cool 
		feature of ejs,we set response in variable “node” and we 			can access it on html as like that (<%= node %>) 
    app.put('/userUpdate', function (req, res) {
        db.updateNode(398, {name:'Prabjot Singh'}, function(err, node){
            if(err) throw err;
            if(node === true){
                // node updated
            } else {
                // node not found, hence not updated
    app.delete('/userDelete', function (req, res) {
        db.deleteNode(398, function(err, node){
if(err) throw err;
            if(node === true){
                // node deleted
            } else {
                // node not deleted because not found or because of existing relationships

Let's Run Server

Almost everything is done,start up you server by executing Server.js (“node Server.js”)

Thank you


About Author

Author Image
Prabjot Singh

Prabjot is a Java and Grails developer.He has a good hands on neo4J, AngularJS. He likes to work on new technologies

Request for Proposal

Name is required

Comment is required

Sending message..