generated from lucien/api-template
Initial commit
This commit is contained in:
commit
00e78c2dff
6 changed files with 997 additions and 0 deletions
33
index.js
Normal file
33
index.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const express = require("express");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const config = require("./config");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const cors = require("cors");
|
||||
|
||||
const app = express();
|
||||
const port = config.port || 3000;
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cookieParser());
|
||||
app.use(cors());
|
||||
|
||||
function loadRoutes(folderName) {
|
||||
const routesPath = path.join(__dirname, folderName);
|
||||
const files = fs.readdirSync(routesPath);
|
||||
files.forEach((file) => {
|
||||
if (fs.lstatSync(path.join(routesPath, file)).isDirectory()) {
|
||||
loadRoutes(`${folderName}/${file}`);
|
||||
return;
|
||||
}
|
||||
const filePath = path.join(routesPath, file);
|
||||
const route = require(filePath);
|
||||
app.use(`/${folderName}/${file.split(".")[0]}`, route);
|
||||
});
|
||||
}
|
||||
|
||||
loadRoutes("api");
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue