example adding URL POST endpoint on the server and sending data to it

This commit is contained in:
Stewart Allen 2022-01-30 10:47:08 -05:00
commit eb98c88d08
2 changed files with 23 additions and 0 deletions

View file

@ -13,5 +13,20 @@ server.onload(() => {
server.util.log("--- called after all modules loaded ---");
});
// adding URL endpoints on the server
server.path.full({
// register the endpoint "/postit"
"/postit": (req, res, next) => {
let chunks = [];
req.on('data', data => {
chunks.push(data.toString());
});
req.on('end', () => {
let data = chunks.join('');
server.util.log({server_received: data});
res.end(`received ${data.length} bytes`);
});
}
});
};

View file

@ -3,4 +3,12 @@ console.log('--- in the browser main loop ---');
// the kiri api starts around line `260` of `src/kiri/main.js`
kiri.load(function(api) {
console.log('--- kiri startup complete ---');
// send data to our "/postit" endpoint
fetch("/postit", {
method: "POST",
body: "this is a test of the POST url endpoint"
}).then(res => res.text()).then(text => {
console.log({server_said: text});
});
});