Skip to content

Instantly share code, notes, and snippets.

@jxpsert
Created July 13, 2022 23:06
Show Gist options
  • Select an option

  • Save jxpsert/19a36133dec85d4e9e9079dcc2b25aca to your computer and use it in GitHub Desktop.

Select an option

Save jxpsert/19a36133dec85d4e9e9079dcc2b25aca to your computer and use it in GitHub Desktop.
import chalk from "chalk";
import fs from "fs";
import express from "express";
// import Endpoint from "./Endpoint.js";
import config from "./config.js";
const app = express();
fs.readdir("./endpoints", (err, files) => {
// get all endpoint files
console.log(chalk.blue(`Found ${files.length} endpoint files: ${files.join(', ')}.`));
files.forEach((file) => {
// for each file
const endpoint = import("./endpoints/" + file);
app.get(`${config.epp}/${endpoint.endpoint}`, (...args) =>
endpoint.get(...args)
);
app.post(`${config.epp}/${endpoint.endpoint}`, (...args) =>
endpoint.post(...args)
);
app.put(`${config.epp}/${endpoint.endpoint}`, (...args) =>
endpoint.put(...args)
);
app.delete(`${config.epp}/${endpoint.endpoint}`, (...args) =>
endpoint.delete(...args)
);
});
});
// FIXME: 404 page
// app.get("*", (req, res) => { // default endpoint
// res.statusCode = 404;
// let endpoint = new Endpoint("");
// res.send(endpoint.format(res.statusCode, "Endpoint not found."));
// });
app.listen(config.port, () => {
console.log(chalk.green(`Listening on 127.0.0.1:${config.port}`));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment