-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (57 loc) · 2.03 KB
/
server.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
59
60
61
62
63
// import modules/packages/dependencies
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const path = require("path");
const PORT = process.env.PORT || 8000;
require("dotenv").config();
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.get("/", async (req, res) => {
try {
// get absolute path of index.html
const root = path.join(__dirname, './index.html');
console.log(root);
// serve index.html file
res.status(200).sendFile(root);
} catch (error) {
console.log("***** Error! Something undesired happened: *****\n", error);
res.status(400).json(error);
}
});
app.get("/transcription", async (req, res) => {
try {
const response = await axios.post(
// use account token to get a temp user token
"https://api.assemblyai.com/v2/realtime/token",
// can set a TTL timer in seconds.
{ expires_in: 3600 },
// AssemblyAI API Key goes here; saved as environment variable for privacy/security
{ headers: { authorization: process.env.ASSEMBLYAI_API_KEY } }
);
// destructure data property from response
const { data } = response;
// create new environment variable to store temporary authentication token
process.env.TEMP_TOKEN = data.token;
res.json(data);
} catch (error) {
console.log("***** Error! Something undesired happened: *****\n", error);
res.status(400).json(error);
}
});
app.get("/healthcheck", async (req, res) => {
try {
console.log(`${process.env.TEST_VARIABLE}`);
console.log("Successfully hitting endpoint /healthcheck");
res.status(200).json("Successfully hitting endpoint /healthcheck");
} catch (error) {
console.log("***** Error! Something undesired happened: *****\n", error);
res.status(400).json(error);
}
});
// start the web server, listening for connections on the port assigned above
const server = app.listen(PORT, () => {
console.log(`***** Server is running on port ${PORT} *****`);
});