Update example (Bun - Deno - Nodejs)

This commit is contained in:
Hassan DRAGA 2024-07-08 19:22:41 -04:00
parent 0440eeaab1
commit 50e9e4c2c6
5 changed files with 95 additions and 11 deletions

View File

@ -0,0 +1,26 @@
// This file gets called like follow:
//
// 1. UI `Index.html` request:
// `http://localhost:xxx/bun_test.ts?foo=123&bar=456`
//
// 2. WebUI runs command:
// `bun run "bun_test.ts" "foo=123&bar=456"`
//
// 3. Bun parses args and prints the response
// Get Query (HTTP GET)
const args = process.argv.slice(2);
const query = args[0];
// Variables
let foo: string = '';
let bar: string = '';
// Read Query
const params = new URLSearchParams(query);
for (const [key, value] of params.entries()) {
if (key === 'foo') foo = value; // 123
else if (key === 'bar') bar = value; // 456
}
console.log('Response from Bun: ' + (parseInt(foo) + parseInt(bar))); // 579

View File

@ -1,7 +1,12 @@
// This file gets called like follow:
// `Index.html` ->
// `http://localhost:xxx/deno_test.ts?foo=123&bar=456` ->
// `deno run --allow-all --unstable "deno_test.ts" "foo=123&bar=456"`
//
// 1. UI `Index.html` request:
// `http://localhost:xxx/deno_test.ts?foo=123&bar=456`
//
// 2. WebUI runs command:
// `deno run --allow-all --unstable "deno_test.ts" "foo=123&bar=456"`
//
// 3. Deno parse args and print the response
// Import parse()
import { parse } from 'https://deno.land/std/flags/mod.ts';
@ -21,4 +26,4 @@ for (const [key, value] of params.entries()) {
else if (key == 'bar') bar = value; // 456
}
console.error('foo + bar = ' + (parseInt(foo) + parseInt(bar))); // 579
console.log('Response from Deno: ' + (parseInt(foo) + parseInt(bar))); // 579

View File

@ -66,7 +66,13 @@
By a simple HTTP request "deno_test.ts?foo=60&bar=40"
<br />
<br />
<button OnClick="call_deno_file();">deno_test.ts (Local file)</button>
<button OnClick="call_deno_file();">Deno_test.ts (Local file)</button>
<br />
<br />
<button OnClick="call_bun_file();">Bun_test.ts (Local file)</button>
<br />
<br />
<button OnClick="call_nodejs_file();">Nodejs_test.ts (Local file)</button>
<br />
<h4><a href="second.html">Second Page As A Simple Link (Local file)</a></h4>
<br />
@ -89,14 +95,33 @@
<script src="/webui.js"></script>
<script>
function call_deno_file() {
// Because `main.c` set Deno as the `.ts` and `.js` interpreter
// then a simple HTTP request to `/deno_test.ts` will be bassed
// to Deno.
function call_deno_file() {
// Note:
// You need to set Deno as default runtime in the backend
// Simple HTTP Request
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', 'deno_test.ts?foo=60&bar=40', false);
xmlHttp.open('GET', 'deno_test.ts?foo=123&bar=456', false);
xmlHttp.send(null);
alert(xmlHttp.responseText);
}
function call_bun_file() {
// Note:
// You need to set Bun as default runtime in the backend
// Simple HTTP Request
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', 'bun_test.ts?foo=123&bar=456', false);
xmlHttp.send(null);
alert(xmlHttp.responseText);
}
function call_nodejs_file() {
// Note:
// You need to set Nodejs as default runtime in the backend
// Simple HTTP Request
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', 'node_test.js?foo=123&bar=456', false);
xmlHttp.send(null);
alert(xmlHttp.responseText);
}

View File

@ -113,7 +113,9 @@ int main() {
// Bind events
webui_bind(MyWindow, "", events);
// Make Deno as the `.ts` and `.js` interpreter
// Set the `.ts` and `.js` runtime
// webui_set_runtime(MyWindow, NodeJS);
// webui_set_runtime(MyWindow, Bun);
webui_set_runtime(MyWindow, Deno);
// Set a custom files handler

View File

@ -0,0 +1,26 @@
// This file gets called like follow:
//
// 1. UI `Index.html` request:
// `http://localhost:xxx/node_test.js?foo=123&bar=456`
//
// 2. WebUI runs command:
// `node node_test.js "foo=123&bar=456"`
//
// 3. Node.js parses args and prints the response
// Get Query (HTTP GET)
const args = process.argv.slice(2);
const query = args[0];
// Variables
let foo = '';
let bar = '';
// Read Query
const params = new URLSearchParams(query);
for (const [key, value] of params.entries()) {
if (key === 'foo') foo = value; // 123
else if (key === 'bar') bar = value; // 456
}
console.log('Response from Node.js: ' + (parseInt(foo) + parseInt(bar))); // 579