iTranslated by AI
XML Object Database with RESTful API | Node.js
Source
Purpose
A database with a RESTful API where you can directly view the contents of objects.
Requirements
- Data manipulation via HTTP requests (GET, POST, PUT, DELETE)
- Data store consists of a single XML file per object
Background
I wanted to create something like an XML version of json-server.
Implementation
Setup
1. Installation
git clone https://github.com/bbsfish/xml-server.git
cd xml-server
npm install
2. Create Configuration File (.env)
/xml-server
├ /lib
├ /data ← Create this folder!
├ .env
└ index.js
PORT = 8999 # Server Port
LOGGER_LEVEL = "all" # Log level for 'log4js'
FOLDER = "./data" # Folder for XML Files
FILEFORMAT = "XML" # File Extention
USE_LOGGING = 1 # Use logging (making backup); 1 is enable, 0 is disable.
LOG_OUTPUT_FILE = "./data/backup.db" # File for logging DB
3. Launch server
node ./index.js
How to use
In the case of the above settings, HTTP requests are made to http://localhost:8999/{id}.
id is a unique value for each resource.
The available HTTP requests are as follows:
1. GET: Retrieve data
2. POST: Register data
3. PUT: Update data
4. DELETE: Delete data
Tutorial
POST
For example, issue an HTTP request like this:
| URL | METHOD | DATA |
|---|---|---|
http://localhost:8999/hogehoge |
POST | {"name": "foo"} |
Then, a file named hogehoge.XML will be generated in the data folder.
<name>
foo
</name>
GET
Next, issue an HTTP request like this.
| URL | METHOD | DATA |
|---|---|---|
http://localhost:8999/hogehoge |
GET |
In this case, if hogehoge.xml exists, an object like {"name": "foo"} will be returned.
PUT, DELETE
Finally, I will explain PUT and DELETE.
PUT is used to update data. If the ID does not exist, nothing is done.
| URL | METHOD | DATA |
|---|---|---|
http://localhost:8999/hogehoge |
PUT | {"name": "foo is updated!"} |
DELETE is used to delete data. If the ID does not exist, nothing is done.
| URL | METHOD | DATA |
|---|---|---|
http://localhost:8999/hogehoge |
DELETE |
Conclusion
There are no filtering features for GET yet. I am currently considering them.
Discussion