iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👌

XML Object Database with RESTful API | Node.js

に公開

Source

https://github.com/bbsfish/xml-server.git

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

.env
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.

hogehoge.xml
<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