Building a Decentralized Pastebin-Like Application with Solid

Numerous social web applications store substantial volumes of data that could be exchanged among them in theory. However, these platforms each maintain proprietary APIs for data storage and access, rendering data reuse across centralized social web platforms unfeasible, even when the datasets are equivalent.


In response to this difficulty, Solid platforms come to the rescue. Solid applications are implemented as client-side Web or mobile applications which read and write data directly from the 'pods'. These pods serve as user-centric data stores, accessible on the Web. The data is accessible for both the client and other pods. Solid enables multiple applications to use the same data on the pod. Users have a decentralized identity called WebID, a unique identifier for users across Solid applications. Moreover, pods have some features that have to be implemented by any pod server, such as data access control and authentication mechanisms.


This tutorial embarks on the creation of a rudimentary Pastebin-like application through the utilization of Node.js. Note that our primary objective is to furnish an elementary application template. Consequently, authentication mechanisms, including login procedures, are intentionally bypassed. Therefore, it is indispensable to exercise caution when considering the application for use in a production environment without careful review.

 

Creating The Solid Server

Solid pods require valid SSL certificates, and in a production environment, using self-signed certificates is not recommended. However, for the purposes of this tutorial and testing, we will skip this prerequisite. To create self-signed certificates, you can simply run the command below, using the default values during the certificate creation process:

openssl req -outform PEM -keyform PEM -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout ./privkey.pem -days 365 -out ./fullchain.pem

Then install a pod provider. We've chosen to use 'solid-server' for its extensibility and suitability for more advanced use cases.

npm install -g solid-server

Before running our pod server, we need to initialize the config files. After we have ran the init command, we used the following variables. You can change them later by editing the config.json file.

solid init

 


Then, we can run our pod server by the following command:

solid start

Creating The Pastebin-Like Application

In this part, we will implement a very basic client application. To start, initialize new Node.js environment in a different folder:

npx init

Then, install two packages. One is for the solid client, and the other one is for serving the client application:

npm install solid-node-client
npm install express

Then, create a new file called 'index.js'. This file will serve our application.

const { SolidNodeClient } = require('solid-node-client');
const client = new SolidNodeClient();

const express = require('express')
const path = require('path');
const app = express()
const port = 3000
app.use(express.urlencoded());
app.use(express.json());

app.get('/', async (req, res) => {
    if (req.query.getter) {
        let readResponse = await client.fetch(`https://localhost:8443/${encodeURIComponent(req.query.getter)}`);
        res.send(await readResponse.text());
    } else {
        res.sendFile(path.join(__dirname, '/index.html'));
    }
})

app.post('/', async (req, res) => {
    const writeResponse = await client.fetch(`https://localhost:8443/${encodeURIComponent(url.parse(req.body.fname))}`, {
        method: "PUT",
        body: req.body.text,
        headers: { "Content-type": 'text/plain' }
    })
    if (writeResponse.status === 201) {
        res.send("success")
    } else {
        res.send(`error: ${writeResponse.status}`)
    }
})

app.listen(port, () => {
    console.log(`Listening on port ${port}`)
})

For our home page, create a file called 'index.html'. Again, it is a very simple application, so we didn't used any fancy CSS code.

<body>
    <form method="get">
        <label for="getter">Get Content:</label>
        <input type="text" id="getter" name="getter"><br><br>
        <button>Get Content!</button>
    </form>
    <form method="post">
        <label for="fname">Name:</label>
        <input type="text" id="fname" name="fname"><br><br>
        <label for="text">Text:</label><br>
        <textarea id="text" name="text" rows="4" cols="50">
        </textarea><br>
        <button>Post Content!</button>
    </form>
</body>

Now we can run our server. Note that if we run our application with self-signed certificates, we will get an error. We will bypass this protection via "NODE_TLS_REJECT_UNAUTHORIZED='0'". We can also add a script to our package.json file for shortcuts. Your package.json file should look similar to the following. Note that version numbers can be different:

{
  "name": "client",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "NODE_TLS_REJECT_UNAUTHORIZED='0' node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "solid-node-client": "^2.1.10"
  }
}

To run the application run the following command:

npm run dev

Now, our application is running.


 

Comments

Popular posts from this blog

Reverse Search Engines

Text To Image Generation With Stable Diffusion XL

ChatGPT in Your Computer: GPT4All and Local Language Models