-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
58 lines (52 loc) · 1.45 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import express from "express";
import { fileURLToPath } from 'url';
import path from 'path';
import cors from "cors";
import main from "./Api.js";
const app = express();
const port = 80;
app.use(express.json());
app.use(cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use("/", express.static(path.join(__dirname, "public")));
// custum middleware
let reqnumber = 0;
app.use((req, res, next) => {
reqnumber = reqnumber + 1;
console.log(`
*New request came :-
method :- ${req.method.toLowerCase()}
request number :- ${reqnumber}
request on :- ${req.originalUrl}
request with :- ${req.get("referrer")}
`);
next();
});
app.post("/apiv1", async (req, res) => {
const message = req.body;
if (!message.message || message.message == " ") {
res.status(400).json({
status: "error",
devmessage: "Dear developer, please note that a message is required !",
});
}
console.log("\n 🤣 User sending :-", message.message);
try {
const { timeing, text } = await main(message);
res.status(200).json({
status: "done",
timeing: timeing,
language: message.language,
message: text,
});
} catch (error) {
res.status(400).json({
status: "error",
devmessage: "Hello, dev Please read all documents find errors, and fix",
});
}
});
app.listen(port, () => {
console.log(`ChatGptApi app listening localhost on port ${port}!`);
});