# Simple Prolog server with JavaScript client

**URL:** <https://swi-prolog.discourse.group/t/simple-prolog-server-with-javascript-client/2898>\
**Category:** Nice to know\
**Created:** [September 9, 2020, 1:17am UTC](https://swi-prolog.discourse.group/t/simple-prolog-server-with-javascript-client/2898 "2020-09-09T01:17:20Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 9, 2020, 9:33am UTC](https://swi-prolog.discourse.group/t/simple-prolog-server-with-javascript-client/2898/2 "2020-09-09T09:33:21Z")

</div>

Nice. 🙂

I like the use of [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) and addressing [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing).

Thanks for finding all of the debug/1 options, e.g. `debug(cookie).`

I was planning on trying a callback to the SWI-Prolog server for use with [Cytoscape.js](https://swi-prolog.discourse.group/t/wiki-discussion-generating-cytoscape-js-graphs-with-swi-prolog/2816/4) so will definitely be looking at your code with a fine tooth comb. Will give feedback as I find it.

* * *

The first bit of feedback is the choice of license.

I find that most JavaScript examples have a MIT license ([ref](https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses)) and the SWI-Prolog uses Simplified BSD license ([ref](https://www.swi-prolog.org/license.html))

* * *

While the GitHub page notes how to use it with SWI-Prolog on Ubuntu it also works on Windows 10.

> **Details**
>
> From DOS prompt
> 
> ```dos
> Microsoft Windows [Version 10.0.19041.450]
> (c) 2020 Microsoft Corporation. All rights reserved.
> 
> C:\Users\Groot>cd "C:/Users/Groot/Documents/swipl-server-js-client"
> 
> C:\Users\Groot\Documents\swipl-server-js-client>swipl simple_server.pl --port=9999 --staticdir=static
> % static dir: static
> % Started server at http://localhost:9999/
> % Starting REPL ...
> ?-
> % [Thread httpd@9999_5] [1] get / ...
> % [Thread httpd@9999_5] [1] 302 moved_temporary(/static/simple_client.html); 394 bytes
> % [Thread httpd@9999_1] [2] get /static/simple_client.html ...
> % [Thread httpd@9999_1] [2] 200 file(text/html,c:/users/groot/documents/swipl-server-js-client/static/simple_client.html); 2,084 bytes
> % [Thread httpd@9999_2] [3] get /static/simple_client.css ...
> % [Thread httpd@9999_2] [3] 200 file(text/css,c:/users/groot/documents/swipl-server-js-client/static/simple_client.css); 40 bytes
> % [Thread httpd@9999_2] [4] get /static/simple_client.js ...
> % [Thread httpd@9999_2] [4] 200 file(text/javascript,c:/users/groot/documents/swipl-server-js-client/static/simple_client.js); 4,161 bytes
> % [Thread httpd@9999_2] [5] get /static/favicon.ico ...
> % [Thread httpd@9999_2] [5] 200 file(image/x-ico,c:/users/groot/documents/swipl-server-js-client/static/favicon.ico); 32,038 bytes
> % [Thread httpd@9999_5] [6] post /json ...
> % [Thread httpd@9999_5] [6] 200 OK (0.016 seconds; 20,168 bytes)
> % [Thread httpd@9999_4] [7] post /json ...
> % [Thread httpd@9999_4] [7] 200 OK (0.000 seconds; 1,811 bytes)
> % [Thread httpd@9999_1] [8] post /json ...
> % [Thread httpd@9999_1] [8] 200 OK (0.000 seconds; 20,168 bytes)
> 
> ```
> 
> Using Microsoft Edge
> 
> ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/0/05597b9bd7aaa12725bc6b5831e95574fabeb528.png)

* * *

For those looking to start the code from the Prolog top level, so they can use make/0 and such, here is a modified predicate

> **Details**
>
> ```prolog
> simple_server_main(Opts0) :-
> dict_create(Opts, opts, Opts0),
> assert_server_locations(Opts),
> http_server([port(Opts.port), workers(5)]),
> debug(log, 'Starting REPL ...', []),
> prolog. % REPL - terminated with exit.
> 
> ```
> 
> Don’t forget to export the predicate:
> 
> ```prolog
> :- module(simple_server,[
> simple_server_main/0,
> simple_server_main/1,
> simple_server_main2/0
> ]).
> 
> ```
> 
> Example run:
> 
> ```prolog
> Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.7)
> 
> ?- working_directory(_,'C:/Users/Groot/Documents/swipl-server-js-client').
> true.
> 
> ?- [simple_server].
> true.
> 
> ?- simple_server_main([port(9999),staticdir(static)]).
> % static dir: static
> % Started server at http://localhost:9999/
> % Starting REPL ...
> % Break level 1
> [1] ?- 
> % [Thread httpd@9999_3] [1] get /static/simple_client.html ...
> % [Thread httpd@9999_3] [1] 304 not_modified; 0 bytes
> % [Thread httpd@9999_3] [2] get /static/favicon.ico ...
> % [Thread httpd@9999_3] [2] 200 file(image/x-ico,c:/users/groot/documents//swipl-server-js-client/static/favicon.ico); 32,038 bytes
> % [Thread httpd@9999_4] [3] post /json ...
> % [Thread httpd@9999_4] [3] 200 OK (0.000 seconds; 141 bytes)
> 
> ```

---

_[View the full topic](https://swi-prolog.discourse.group/t/simple-prolog-server-with-javascript-client/2898)._
