• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Client - Server API for a website

Hey guys, so im refactoring my websites code. Right now it has the various string commands that make up the API stored in a class called ServerAPI.java on the server and a javascript file called ServerAPI.js for the client. So basically its two files with the exact same string values...

I was thinking about having the client side ServerAPI.js deriving its values directly from ServerAPI.java by using an ajax query to get them in JSON form or something like that. That way I dont have to maintain two separate files. What do you guys think? Is this a smarter plan?

Also what do you think about keeping the API stored in localstorage so it can be used on various html pages without having to refetch it from the server each time? Any pitfalls to this?
 
It feels to me like you might be over-thinking this. The API doesn't change much, right? What about including the API in the ServerAPI.js file that gets served to clients with some kind of server-side include? Are you using Java Server Pages?
 
Im using Java for the backend and HTML5/JavaScript on the frontend.

The server stores the API like so:
ACw43rC.jpg


The client gets the API as part of GlobalValues.js here, its in the serverAPI variable:
zBWOsCH.jpg


I guess what im looking for is a way to ensure that JavaScript file is kept in line with the ClientAPI.java class. You're right though the API dosent change much so I suppose its not a big deal.
 
OK, alternatively, the JSON could be in its own .js file, and Java could load it, strip any excess code like a leading variable assignment, and parse it as JSON in Java.

It really seems like you should have some JS preprocessing in there, though. If for no other reason than to do file consolidation and automatic minification.
 
Back
Top