2019-01-19 19:41:18 +01:00
|
|
|
# Translucid
|
2024-05-28 03:35:22 +02:00
|
|
|
|
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:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
npm install translucid --save
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, you need to include it in your project:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
const trans = require("translucid");
|
|
|
|
```
|
|
|
|
|
2019-01-19 22:32:08 +01:00
|
|
|
To create your first server, call ".QuickServer(port)":
|
2024-05-28 03:35:22 +02:00
|
|
|
|
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:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-19 22:23:09 +01:00
|
|
|
```
|
|
|
|
const {app,server,connect,translucid} = trans.QuickServer(8000);
|
|
|
|
```
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-19 22:32:08 +01:00
|
|
|
The translucid object can be used to make file bindings:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
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
|
|
|
```
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-19 22:34:32 +01:00
|
|
|
Then you can add middleware like this:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
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:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-28 19:01:16 +01:00
|
|
|
```
|
|
|
|
translucid.public(`client`);
|
|
|
|
```
|
|
|
|
|
|
|
|
You can read bindings from a file:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-28 19:01:16 +01:00
|
|
|
```
|
|
|
|
translucid.bindJSON(`data/files.json`);
|
|
|
|
```
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-28 19:01:16 +01:00
|
|
|
And in files.json:
|
2024-05-28 03:35:22 +02:00
|
|
|
|
2019-01-28 19:01:16 +01:00
|
|
|
```
|
|
|
|
{
|
|
|
|
"/articles":{
|
|
|
|
"file":"client/articles.html",
|
|
|
|
"classes":["*"]
|
|
|
|
},
|
|
|
|
"/":{
|
|
|
|
"file":"client/start.html",
|
|
|
|
"classes":["*"]
|
|
|
|
},
|
|
|
|
"/start":{
|
|
|
|
"file":"client/start.html",
|
|
|
|
"classes":["*"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-05-28 03:35:22 +02: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
|
|
|
|
|
|
|
|
Other utilities in translucid:
|
2019-01-28 19:01:16 +01:00
|
|
|
|
2024-05-28 03:35:22 +02:00
|
|
|
- read => read a file
|