1

My question is about how to create a definition for an object that is handled by different web services.

A brief description of my current scenario and why I think it's a problem:

A PHP web application provides AJAX endpoints for populating various parts of my web app. The endpoint provides access to User and Project entities. A node.js application uses sockjs to deliver User and Project events in real time to my web app.

Here's the problem: The schema of the JSON objects delivered by the node.js service is slightly different to the same objects provided by the PHP endpoints. This is bad because it makes much my client size code hard to re-use, and there's lots of "if it came from node, access this property; if it came from AJAX, access this other property" type logic. Ugh!

The code base is too big to do a one time refactor and make sure all devs adhere to the new design.

I want a way to share information between my node.js app and my php app so they both know how they should define certain objects.

What is the most common way of solving this kind of problem?

asked Jun 21, 2013 at 6:55
1
  • You could use a single wrapper for both php and node.js. But again this requires big refactoring. Commented Jun 21, 2013 at 12:18

1 Answer 1

1

Normalize any outgoing JSON from your node.js webservice.

For example, for each schema you have, create a wrapper function that modifies your JSON data to match the format of your PHP data. Even if you just define a simple translation like this:

function convertUser(user) {
 return {
 id: user._id,
 anotherField: user.another_field,
 // ...
 };
}

My bigger concern is the reason for this. Are you just dumping data directly from the database? This sort of normalization should be part of the dump process rather than blindly giving back whatever is requested. As database fields (or schemas, in your case) change, your API should not.

answered Jun 21, 2013 at 15:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.