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
|
|
|
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) => {
|
2019-01-28 19:01:16 +01:00
|
|
|
next(`${prev} <br/> string added by a middleware`);//passing the argument is optional
|
2019-01-19 22:23:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2019-01-28 19:01:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
You can make a folder public like this:
|
|
|
|
```
|
|
|
|
translucid.public(`client`);
|
|
|
|
```
|
|
|
|
|
|
|
|
You can read bindings from a file:
|
|
|
|
```
|
|
|
|
translucid.bindJSON(`data/files.json`);
|
|
|
|
```
|
|
|
|
And in files.json:
|
|
|
|
```
|
|
|
|
{
|
|
|
|
"/articles":{
|
|
|
|
"file":"client/articles.html",
|
|
|
|
"classes":["*"]
|
|
|
|
},
|
|
|
|
"/":{
|
|
|
|
"file":"client/start.html",
|
|
|
|
"classes":["*"]
|
|
|
|
},
|
|
|
|
"/start":{
|
|
|
|
"file":"client/start.html",
|
|
|
|
"classes":["*"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-01-28 19:05:35 +01:00
|
|
|
The QuickServer returns:
|
|
|
|
* translucid => the basic translucid object
|
|
|
|
* express => the express module
|
|
|
|
* http => the http module
|
|
|
|
* server => instance of http.Server()
|
|
|
|
* connect => a promise that resolves when the server is listening to the specified port
|
2019-01-28 19:01:16 +01:00
|
|
|
|
2019-01-28 19:05:35 +01:00
|
|
|
Other utilities in translucid:
|
|
|
|
* read => read a file
|