I have 3 parts of project
- -bot
- -client
- -server
Bot just open Telegram mini app. i
And when i try to send request to local server on http://localhost:3000 is is write ERR_CLEARTEXT_NOT_PERMITTED.
I find that it is cause mobile devices cannot send HTTP, only HTTPS.
I generate cert using openssl
This is my server.
import express, { Request, Response } from "express";
import sequelize from "./config/database";
import { User } from "./models/user";
import cors from "cors";
import https from "https";
import * as fs from "fs";
import * as path from "path";
const app = express();
const port = 3000;
app.use(express.json());
const corsOptions = {
origin: "http://localhost:5173",
credentials: true, //access-control-allow-credentials:true
optionSuccessStatus: 200,
};
app.use(cors(corsOptions)); // Use this after the variable declaration
app.get("/", async (req: Request, res: Response) => {
res.send("Hello World!");
});
app.post("/user", async (req: Request, res: Response) => {
const { first_name, username, id } = req.body;
console.log(req.body);
try {
// Check if user already exists
const existingUser = await User.findOne({ where: { id } });
if (existingUser) {
return res.status(409).json({ error: "User already exists" });
}
// Create new user
const user = await User.create({ first_name, username, id });
res.status(201).json(user);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
console.log(__dirname);
const options = {
key: fs.readFileSync(path.resolve(__dirname, "../cert/key.pem")),
cert: fs.readFileSync(path.resolve(__dirname, "../cert/cert.pem")),
};
// app.set("ssl", true);
// app.set("sslKey", options.key);
// app.set("sslCert", options.cert);
const start = async () => {
try {
await sequelize.authenticate();
console.log("Database connected!");
await sequelize.sync({ force: true }); // This will drop existing tables and re-create them
// const server = https.createServer(options, app);
// server.listen(port, () => {
// console.log("Express server listening on port " + port + ", secure = " + true);
// });
app.listen(port, () => {
console.log("Express server listening on port " + port + ", secure = " + true);
});
} catch (error) {
console.error("Unable to connect to the database:", error);
}
};
start();
It is happen only with mobile, cause when i open miniApp through Desktop Telegram, everything is working and requests sending to the server
I try to avoid ERR_CLEARTEXT_NOT_PERMITTED but when i change http to https request from client, i start to get another error **https://localhost:3000/net::ERR_CONNECTION_REFUSED
**
Maybe some triubles with certificates, or somebody know how to avoid ERR_CLEARTEXT_NOT_PERMITTED error from mobile?
P.S
I using simple webpage Vite on client, it is not some Android studio