Created
November 19, 2022 01:23
-
-
Save kiprasmel/880f2ad95a47e2a16b406edb9d175a6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import fs from "fs"; | |
import { Server } from "http"; | |
import express from "express"; | |
import cors from "cors"; | |
const app = express(); | |
app.use(cors()); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: false })); | |
app.post("/perform", async (req, res, next) => { | |
const { foo } = req.body; | |
try { | |
res.status(200).json({ foo }); | |
return; | |
} catch (e) { | |
next(e); | |
} | |
}); | |
export const defaultConfig = { | |
port: 3000, | |
hostname: "localhost", | |
}; | |
export function startServer({ | |
port = defaultConfig.port, // | |
hostname = "localhost", | |
} = {}): Server { | |
const server = app.listen(port, hostname, async () => { | |
const msg = `listening on "http://${hostname}:${port}" @ NODE_ENV "${process.env.NODE_ENV}"`; | |
console.log(msg); | |
}); | |
return server; | |
} | |
if (!module.parent) { | |
startServer(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment