portl v0.0.10
portL Microserver
portL converts a node.js file into a secure https server. All the exported functions are automatically callable from the frontend.
Quick Start
Install
Install the package with:
npm i portl
or global with:
npm i -g portl
Files
serve.js
First specify an exported function that you want to serve.
const simple = (name) => `Hello ${name}`;
module.exports = { simple };
users.htpasswd
Then create a file containing all the user passwords for access.
user:pw
user2:pw2
For example the usernames user
/ user2
and the passwords pw
/ pw2
index.html
Once your imported the automatically generated script https://localhost/portl.js
. You can use all the exported functions like so:
<!DOCTYPE html>
<html lang="en">
...
<body>
<script src="https://localhost/portl.js"></script>
<script>
portl.simple("World").then(console.log);
// => Hello World
</script>
</body>
</html>
CLI
Just run the portl
command as npm script or in the terminal.
the default files are:
file to serve | password file |
---|---|
./serve.js | ./users.htpasswd |
You can optionally define other files:
portl [file to serve] [password file]
Async Resolves
For asynchronous resolves just return a promise on the server side.
serve.js
const timeout = () =>
new Promise((resolve) => setTimeout(() => resolve("1s passed"), 1000));
module.exports = { timeout };
index.html
<!DOCTYPE html>
<html lang="en">
...
<body>
<script src="https://localhost/portl.js"></script>
<script>
portl.timeout().then(console.log);
// 1s ... => 1s passed
</script>
</body>
</html>
Subscriptions
Subscriptions are nested functions the first parenthesizes are from the client side (called with portl.timeSubscription()
) whereas the second one is from the server side (also called with portl.timeSubscription()
).
serve.js
const timeSubscription = (prefix) => (time) => prefix + time;
const getTime = () => {
portl.timeSubscription(Date.now());
setTimeout(getTime, 1000);
};
getTime();
module.exports = { timeSubscription };
index.html
<!DOCTYPE html>
<html lang="en">
...
<body>
<script src="https://localhost/portl.js"></script>
<script>
portl
.timeSubscription("time in ms: ")
.then((value, abort) => console.log(value));
// every second => time in ms: 15776...
</script>
</body>
</html>
Good to Know
You have to use this exact format of arrow functions: (...) =>
. Don't leave out the parenthesizes for now.
TODO
- SSL certificate
- self signed
- real
- better subscription model