Install your own oData server with nodejs and MongoDB

Author: Gábor Dolla
19 June 2013

node.js, MongoDB, OData, examples


Update: it does work on windows from 1.2.4 !!!!

Using a remote datasource from the browser with jaydata is as easy as pie if your datasource speaks oData.  Unfortunately database servers don’t but with jaydata it is possible to use a database server as an oData endpoint. In this example I use MongoDB as the database server. In the following months jaydata will support many sql servers as backend so you will be able to access your data stored in your mysql, mssql, oracle, etc. server using oData.

Please note that this runs on Linux and OSX and will not run on Windows. In this example I use Ubuntu Server 12.04.


First install the necessary tools and libraries:
sudo apt-get install mongodb
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get install nodejs nodejs-dev
sudo apt-get install  npm build-essential make autoconf libtool flex bison git libxml2-dev

Let’s create a new project:
mkdir test; cd test
npm install mongodb express http connect q node-uuid bcrypt xmldom qs
npm install jaydata

Let's define the model:


$data.Class.define("test.Product", $data.Entity, null, {
    Id: { type: "id", key: true, computed: true, nullable: false },
    Name: { type: "string" },
    Price: { type: "integer" }
}, null);
$data.Class.defineEx("test.Context", [$data.EntityContext,$data.ServiceBase], null, {
    Products: { type: $data.EntitySet, elementType: test.Product }
exports = test.Context;


Only a few lines of code is needed to start up a webserver and make the database accessible:


var c = require('express');
var app = c();
app.use(c.session({ secret: 'session key' }));
app.use("/test", $data.JayService.OData.Utils.simpleBodyReader());
app.use("/test", $data.JayService.createAdapter(test.Context, function (req, res) {
    return new test.Context({name: "mongoDB", databaseName:"test", address: "localhost", port: 27017 });
app.use("/", c.static(__dirname));

If you start up the server and visit http://localhost:8080/test/$metadata then you’ll find the metadata.



When you see a message on the console saying: “Should be empty: []” please ignore it. It comes from ‘q’ promise and it is harmless.


Let’s create a simple client for our new service:


Download jaydata from and unzip it under scripts folder, download datajs from, after copying the files, scripts folder should look like this:




Let’s write a simple client:


<!DOCTYPE html>
    <meta charset="utf-8">
    <script src="scripts/datajs-1.0.3.js"></script>
    <script src="scripts/jaydata/jaydata.min.js"></script>
    <script src="scripts/jaydata/jaydataproviders/oDataProvider.js"></script>
     $data.service('/test', function (contextFactory, contextType) {
       var context = contextFactory();
       context.Products.toArray(function (items) {


Open http://localhost:8080/index.html look at the console. You won’t find much there as the database is empty. Let’s put some data into the database:




Refresh the browser and the products will be listed on the console.


Having a direct connection from the browser to the database is really powerful but also raises questions about security. In JayStorm we’ve implemented a security middleware, you can read more about it here:


Of course, you can use your new oData endpoint from other oData clients including .Net, Java, Excel, mobile devices, etc. You can even you it as a REST endpoint so AJAX clients can also be served.

You can find more examples on how to use jaydata and on a new site created by our friend Rainer.

Finally two pieces of good news:
1. If you want to use oData with database backend without setting up your own environment, you can use our infrastructure at a very competitive price. Just visit and sign up for JayStorm service which gives you all of this and much more.
2. We’re working on a prebuilt solution, which can be run on all the popular virtualization platforms including VMWare, HyperV, VirtualBox, AWS, Azure.

Share this

categories: JayData examples, MongoDb, node.js


  • Missing user avatar

    I'm using Ubuntu Server 12.04 and I'm trying to build an OData endpoint with nodejs and jaydata following the instructions you give but when the instruction require('jaydata'); is executed node returns Should be empty: []

    Am I missing something?

    Thanks in advance.


  • Missing user avatar

    Forgot to mention that I'm using node version 0.8.14 and jaydata 1.2.3

  • Missing user avatar

    We do not check comments often so please use our forum for questions.

    'Should be empty: []' on the console comes from a library that we use, q promise.
    It's harmless, causes no problem whatsoever.

    Since the blogpost was published we made some improvements so you can also run it on windows.


  • Missing user avatar

    Really Powerful!!!

    Do you have git project for the sample code to fork?


Leave a comment

comments powered by Disqus

Deliver your project faster with JayData, get professional developer support! Learn more