This is a mobile optimized page that loads fast, if you want to load the real page, click this text.

API для RedAge V3

Voice

Участник портала
22 Сен 2023
26
10
45
Нужен человек который сможет написать API для сервера.

API на вывод логинов игроков для валидации(проверка логина на его существование в базе)
API на вывод онлайна на сайт

мой дискорд - voiceboyce
 

mippoosedev

Гуру
BackEnd developer
2 Мар 2021
303
149
100
И никакое API не нужно

JavaScript:
const serverIp = "IP:PORT"
    function getOnline() {
        return fetch('https://cdn.rage.mp/master/')
            .then((response) => response.json())
            .then((responseJson) => {
                setCurrentOnline(responseJson[serverIp].players)
            })
            .catch((error) => {
                console.error(error);
            });
    }
 

mippoosedev

Гуру
BackEnd developer
2 Мар 2021
303
149
100
А вот тебе и мини апи(не проверял если шо, подредачьте запрос если нужно

П.С у себя на другом запросе проверил - вроде всё гуд
П.С.С Я не тру жабаскрипт кодер, не бейте тапками за апишку-двухминутку


package.json

JSON:
{
  "name": "redage-login-info-api",
  "version": "0.0.1",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "ragemp.pro",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.18.2",
    "mysql2": "^3.6.5"
  }
}

index.js

JavaScript:
const express = require("express");
const db = require("./db");
const app = express();
const port = 3000;

app.use(express.json());

app.use(
    express.urlencoded({
        extended: true,
    })
);

app.get("/", (req, res) => {
    res.json({ message: "api is fine..." });
});

// GET /occupied?username=...
app.get("/occupied", async (req, res) => {
    try {
        const username = req.query.username;
        const results = await db.query("SELECT * FROM accounts WHERE login = ?", [username]);
        res.status(200).json({accounts: results.length});
    } catch (e) {
        res.status(500).json({error: e});
    }
})

app.listen(port, () => {
    console.log(`API listening at http://localhost:${port}`);
});

db.js

JavaScript:
const mysql = require('mysql2/promise');

const dbConfig = {
    host: "host",
    user: "user",
    password: "pass",
    database: "database",
    connectTimeout: 60000
}

let connection;

async function query(sql, params) {
    if (!connection || connection.state === 'disconnected') {
        connection = await mysql.createConnection(dbConfig);
    }
    const [results, ] = await connection.execute(sql, params);
    return results;
}

module.exports = {
    query
}
 

Voice

Участник портала
22 Сен 2023
26
10
45
спасибо) проверю)

отпишусь) тему закрывать не буду, вдруг кому нужно будет
 

Similar threads