monstercanker/node_api/api/index.js

48 lines
1.1 KiB
JavaScript

require('dotenv').config()
const express = require("express")
const path = require("path")
const cors = require("cors")
const mlib = require("../../node_lib")
let app = null
const { LISTENING_PORT } = process.env
const PORT = LISTENING_PORT || 3000
async function main() {
app = express()
app.use(cors())
app.use(express.json())
app.get("/api", async (req, res) => {
let { random } = req.query
// try to parse random, can be a number or a boolean
if (random) {
if (random === "true") {
random = true
} else if (Number(random)) {
random = Number(random)
}
}
const phrases = await mlib({ random })
res.json(phrases)
})
app.use(express.static(path.join(__dirname, "..", "web", "dist",)))
// serve static react build
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "..", "web", "dist", "index.html"))
})
app.listen(PORT)
console.log(`Listening on port ${PORT}`)
}
main().catch(console.error)