How to set up a nodejs OData endpoint with odata-server

Author: Robert Bonay April 3rd, 2013

, , ,

After your very positive feedback to server-side JayData implementation we decided to help you to create OData-enabled endpoints with much less code by releasing the odata-server npm module.
OData-server module comes handy if want to create a node.js service with standard communication protocols (RESTful OData with XMLS/JSON support). The system comes with an out-of-the-box MongoDB data provider, but you can use SQLite or extend the service with your custom JayData data provider. The beauty of the module is that you can use JayData JavaScript Language Query to manipulate object on the server-side, just as you do with JayData library on the client-side – while writing developer-friendly, pure JavaScript code.

We show how will you able to create your CRUD-capable OData endpoint with 4 calls:

Save this code to todo.js.

You can use odata-server to publish JavaScript Data Services, which is similar to IQueryable results of WCF Data Services; or JavaScript API Services similar to WCF service methods.

Installing odata-server

The module can be found in npm, so you just have to type the following command:

Warming up the odata-server

The module comes with two example services: newsreader and northwind, let’s launch our todo one.
After starting the services with node todo.js   command, the OData endpoint will be published with initialized MongoDB schema, this means you can access your services by opening the following URL in your browser:

  • http://localhost:52999/todo

Todo example explained

As a client-side JayData application, server-side services begin with the definition of typed entities.

By defining the Todo this way, the value of the Id field of the object will be generated on the server.

(To learn more about JayData visit the Beginners guide to JayData

After defining the Todo entity, the second step is to declare the EntityContext to enlist the sets of your entities. As we don’t have multiple objects and object relations, we will have the Todos EntitySet here:

Initializing your todo service takes only two lines of as $data.createODataServer() function encapsulates all the necessary operations based on 4 parameters:

  • Your custom EntityContext
  • The relative URL of the desired service
  • The port to listen
  • The hostname of the service

After starting the service code saved to your todo.js you will have a working OData endpoint, which can be accessed using JayData or any other OData client. Have you ever seen any easier way to set up an OData end-point? Okay-okay, JayStorm PaaS doesn’t count! 🙂

The odata-server is compatible with Linux and Windows platforms and you can host it on your own infrastructure or in cloud IaaS like Microsoft Windows Azure or Amazon Web Services.

Try the JayData-powered odata-server now! – https://npmjs.org/package/odata-server

Using your todo service

Your todo endpoint can be discovered by typing the http://localhost:52999/todo/$metadata URL to the address bar of your browser.


This is the standard OData metadata descriptor of your endpoint, which enlists all the published types, EntitySets and service methods.

After all of this, it’s time to save and query records from a HTML5 client using JayData library.

It’s important to mention that filtering of the records happens on the server by translating the JavaScript .filter() expression to OData query string and JayData parses the results to typed entities, so you can work with results as objects.


Stay tuned to see odata-server operating with geospacial data!

, , ,