Skip to content

Repository files navigation

JTP (JSON Transfer Protocol) Library

Easy to use, fast and lightweight library for Node.js.

LICENSE NPM version NPM downloads

🚧 This project is under development and is not yet ready for use.

🔗 Table of Contents

💡 About

JTP is a protocol for transferring data between systems. It facilitates communication between systems using JSON (JavaScript Object Notation) as the data format and relies on the TCP/IP protocol for data transfer.

JTP Request Example

{
  "head": {
    "host": "example.com:6969",
    "method": "CREATE",
    "path": "/api/v1"
  },
  "body": {
    "type": "string",
    "data": "Hello World!!"
  }
}

🚀 Tecnologies

The following tools were used in the construction of the project:

👷 How to run

Installation

npm install @geisonjr/json-transfer-protocol

or using yarn

yarn add @geisonjr/json-transfer-protocol

📖 Documentation

Configure the env file for the client

# Host address of the client
CLIENT_HOST=127.0.0.1
# Port the client will use
CLIENT_PORT=6969
# Indicates if the client uses a secure connection
CLIENT_SECURE=true

Configure the env file for the server

# Host address of the server
SERVER_HOST=127.0.0.1
# Port the server will use
SERVER_PORT=6969
# Indicates if the server uses SSL for secure connections
SERVER_SECURE=true
# Path to the server SSL certificate
SERVER_CERT=/certs/cert.pem
# Path to the server SSL key
SERVER_KEY=/certs/key.pem
# Path to the Certificate Authority (CA) certificate
SERVER_CA=/certs/ca.pem

Configure the env file for the logger

# Directory where logs will be stored
LOG_DIR=./logs
# Enables or disables logging
LOG=false
# Maximum size of the log file
LOG_MAX=1000000
# Defines if logs will be displayed in the console
LOG_CONSOLE=true
# Defines if logs will be formatted as JSON
LOG_JSON=false
# Defines if logs will be formatted as text
LOG_LOG=false
# Enables or disables DEBUG log level
LOG_DEBUG=true
# Enables or disables INFO log level
LOG_INFO=true
# Enables or disables WARN log level
LOG_WARN=true
# Enables or disables ERROR log level
LOG_ERROR=true
# Enables or disables FATAL log level
LOG_FATAL=true
# Enables or disables TRACE log level
LOG_TRACE=true

Create a server

import { Server, Status } from '@geisonjr/json-transfer-protocol';

const server = new Server({
  port: 6969
});

server.start();

server.create('/api/test', (req, res) => {

  const name = req.body.data.name;

  res.status = Status.OK;
  res.body = {
    type: 'string',
    data: `Hello, ${name}`
  };
});

Create a request

import { fetcher } from '@geisonjr/json-transfer-protocol';

const response = await fetcher({
  head: {
    host: 'example.com',
    method: 'CREATE',
    path: '/api/test'
  },
  body: {
    type: 'object',
    data: {
      name: 'Geison',
      age: 23
    }
  }
});

if (!response.success) {
  console.error(response.data);
  return;
}

console.log(response.data);

🤝 How to Contribute

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

📝 License

This project is under the MIT License.

About

A simple transfer protocol proposal using JSON

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages