Add a route to a 'server' edition webserver

My purpose is build web applications on top of the out-of-box TiddlyWiki module (‘npm install tiddlywiki’). TiddlyWiki ‘server’ edition provides the required synchronization between browser front-end and the server side wiki.

The first step to add application specific features to server side ‘server’ edition is to create a tiddler with module-type ‘route’. See built-in tiddler $:/core/modules/server/routes/get-favicon.js as an example of what a ‘route’ tiddler looks like.

Simple enough. So add a module-type: ‘route’ tiddler with the following (… in place of details):

exports.method = "GET";
exports.path = /^\/addtag\/.../;
exports.handler = function(request,response,state) { ... }

which is a route to have the server add a tag to a tiddler. Silly, but as an example. Path would be ‘/addtag/:tiddler/:tag’.

Here is where I am probably incorrect. To add the app specific /addtag HTTP request browser side; I need to override the $:/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js shadow tiddler to implement the new route, by adding :

TiddlyWebAdaptor.prototype.addTag = function() { ... }

Is my head too deep in the weeds - or is there a way to add the route HTTP request brower side without overriding a shadow tiddler?

1 Like

Hi @poc2go you are correct about the server side implementation details. The problem is that if you did add your TiddlyWebAdaptor.prototype.addTag method there isn’t any core code that would know to call it. Sync adaptors like TiddlyWebAdaptor have a small set of standard methods that are required in order for the syncer module to be able to sync to them.

I know addTag was just an example, but it is a good example of the sort of method that would be very hard to integrate into TW. On the client side, the wiki store has no notion of “adding tags”, just of updating the values of tiddlers. So if you wanted your addTag method to be called whenever a tag is added to a tiddler you’d actually need a bunch of logic to track the tags on the tiddlers you’re interested in and monitor for changes.

In general, I think the best approach to calling a custom server API on the client is often to have a custom UI that is responsible for invoking it. Nowadays that sort of thing can often be done entirely in wikitext, using the HTTP request message to invoke the server APIs.

Why not doing changes using action widgets, and let sync adaptor’s existing syncing feature to sync changes for you?

Usually adding server-side route is for 3rd party applications, like tw-mobile-sync add a new POST endpoint

because TidGi-mobile app will use it.