Express Functions
An expressjs library for making it easy to connect backend and frontend of modern web applications with functions.
Getting started
First, we need to setup our backend.
Backend
Install express and express-functions
$ npm install express express-functions
Create our server files
// ./app.js const express = ;const path = ;const exprFn = ; const app = ; // for serving the index.html and frontend js fileapp; // import the server functions// we're not gonna use events nowapp; // start the serverconst port = processenvPORT || 8080;app;
// ./functions.js // export an object with your functionsmoduleexports = { return `Hello , you're welcome!`; } { // use object-destructuring for return a + b; // parameters }// ... thats it!
Frontend
For the frontend we need to add the frontend file. You can find it at the releases tab on github.com. Just download the file or use a CDN (used in the example).
Now the index.html file
<!-- ./public/index.html --> ExpressFunctions <!-- Add express-functions script -->
And thats all you got to do for getting your frontend and backend to communicate via functions. You can transfer data, work with it on the server and send it back to the user.
Using Events
If you want to use events, we need to set-up a bit more mostly on the front-end.
First, on the back-end
// app.js // ... app; // boom, thats all // ...
The Front-End
Lets trigger an event and catch it again. This doesn't make much sense if it happens on the same page, but it gets the point across...So lets edit the index.html file:
<!-- ./public/index.html --> <!-- ... --> <!-- ... -->
And as expected, every second a new number appears on the screen.
Backend Docs
The express-functions
package exposes a function to use. Add it to express
via app.use(require('express-functions')(/* options */));
.
Options
events
(Boolean):true
if you want to use events,false
if not.functions
(Object)[optional]: if specified, enter an object with functions as attributes. You can call these functions by their name from the front-end later. Use object-destructuring for parameters.
app;
Frontend Docs
- ####
exprfn.connect([backend-domain])
Call this function to connect your front-end application with your back-end. This is an async function, so I advise to use it like this:
const backend = await exprfn;
The connect function takes the domain of your back-end server as an
optional argument: await exprfn.connect('https://your.domain.com');
It returns an object with the following functions:
- ####
.call({functionName}, [functionParams])
The call function lets you call functions which you specified in your express-functions configuration on your server. This is an async function. ThefunctionName
is a required parameter (string). ThefunctionParams
(object) obviously depend on the function you specified on your server.
console // => 8
- ####
.once({eventName})
Theonce
function is used with events. With this you listen for an event with the giveneventName
(string). This is an async function. The data which the event is triggered with will get resolved once.
// examplebackend
- ####
.on({eventName}, {callback});
Theon
function is used with events. With this you can listen for events beeing fired off multiple times.
// examplebackend
- ####
.emit({eventName}, [data]);
The emit function will trigger an event on the server. Every request for listening to thiseventName
(string) will get resolved. The optionaldata
(object) will be passed to the receiver if provided.
// senderbackend;
// receiverbackend;