Skip to content

Commit

Permalink
Fix loading endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Jan 15, 2024
1 parent a3a6376 commit cce7cde
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 12 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ if (!existsSync('./src/files')) {

const app = express();
try {
app.use(fileUpload());
const result = loadEndpoints(app);
if (result !== undefined) {
otherMessage(`Loaded ${result.loaded} endpoints`);
} else {
otherMessage('No endpoints found');
}
app.listen(PORT, () => {
otherMessage(`Server started on port ${PORT} @ http://localhost:${PORT}`);
});
(async () => {
app.use(fileUpload());
const result = await loadEndpoints(app);
if (result !== undefined) {
otherMessage(`Loaded ${result} endpoints`);
} else {
otherMessage('No endpoints found');
}
app.listen(PORT, () => {
otherMessage(`Server started on port ${PORT} @ http://localhost:${PORT}`);
});
})();
} catch (error) {
errorMessage(`Error starting server: ${error}`);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.0.0",
"main": "index.ts",
"scripts": {
"start": "npm run build && node dist/index.js",
"start": "npm run build && cd dist/ && node index.js",
"prettier": "npx prettier --write index.ts src/",
"prettier:check": "npx prettier --check index.ts src/",
"lint": "npx eslint --fix index.ts src/",
Expand Down
14 changes: 6 additions & 8 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ import { otherMessage, errorMessage } from './logger';
import { Application } from 'express';
import { readdirSync } from 'fs';

export const loadEndpoints = (app: Application) => {
export const loadEndpoints = async (app: Application) => {
try {
const items = readdirSync('./src/endpoints');
let loaded = 0;
for (const item of items) {
(async () => {
const endpoint = await import(`./endpoints/${item}`);
endpoint.default(app);
loaded++;
otherMessage(`Loaded ${item.split('.')[0]} endpoint`);
})();
const endpoint = await import(`./endpoints/${item}`);
endpoint.default(app);
loaded++;
otherMessage(`Loaded ${item.split('.')[0]} endpoint`);
}
return { loaded };
return loaded;
} catch (error) {
errorMessage(`Error loading endpoints: ${error}`);
}
Expand Down

0 comments on commit cce7cde

Please sign in to comment.