Client - Server API for a website

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
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?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,603
4,521
75
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?
 

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
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.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,603
4,521
75
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.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I'd prefer to keep it in Java and generate JSON or JS from the Java.