2019-01-19 19:41:18 +01:00
|
|
|
# Translucid
|
2019-01-19 22:32:08 +01:00
|
|
|
A simple node.js library to bind files to requests
|
2019-01-19 22:23:09 +01:00
|
|
|
|
|
|
|
# Get started:
|
|
|
|
|
2019-01-19 22:32:08 +01:00
|
|
|
First you need to install the package:
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
npm install translucid --save
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, you need to include it in your project:
|
|
|
|
```
|
|
|
|
const trans = require("translucid");
|
|
|
|
```
|
|
|
|
|
2019-01-19 22:32:08 +01:00
|
|
|
To create your first server, call ".QuickServer(port)":
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
trans.QuickServer(8000);
|
|
|
|
```
|
|
|
|
|
2019-01-19 22:32:08 +01:00
|
|
|
The ".QuickServer" method return many useful objects:
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
const {app,server,connect,translucid} = trans.QuickServer(8000);
|
|
|
|
```
|
2019-01-19 22:32:08 +01:00
|
|
|
"Connect" is a promise that resolves when the server starts listening to the port:
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
connect.then(() => {
|
|
|
|
console.log("Listening on port 8000!");
|
|
|
|
});
|
|
|
|
```
|
2019-01-19 22:32:08 +01:00
|
|
|
The translucid object can be used to make file bindings:
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
2019-01-20 11:57:11 +01:00
|
|
|
translucid.bind("/","client/index.html",["myId"]);
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
2019-01-19 22:34:32 +01:00
|
|
|
Then you can add middleware like this:
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
translucid.use({
|
2019-01-19 22:34:32 +01:00
|
|
|
name:"my middleware",
|
2019-01-19 22:25:24 +01:00
|
|
|
keys:["myid"],
|
2019-01-19 22:23:09 +01:00
|
|
|
run:(prev,req,res,next) => {
|
|
|
|
//prev is the data that is going to be sent to the client
|
|
|
|
//(it might for example be the index.html file);
|
2019-01-19 22:34:32 +01:00
|
|
|
next(`${prev} <br/> string added by a middleware`);
|
2019-01-19 22:23:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|