Implemented more loaders

ref #4
This commit is contained in:
Nicolai Ort 2020-11-25 18:31:06 +01:00
parent e3883fecbd
commit 6ce88a1e3d
5 changed files with 44 additions and 25 deletions

View File

@ -7,21 +7,17 @@ import bodyparser from "body-parser";
import 'reflect-metadata';
import routerMain from './routes/index';
// const loaders = require('./loaders');
import * as loaders from './loaders/index';
import loaders from './loaders/index';
dotenvSafe.config();
const app = express();
let app = express();
const PORT = process.env.APP_PORT || 4010;
createConnection()
.then((connection) => {})
.catch((err) => {
consola.error(err);
})
.finally(() => {
// await loaders.init(app);
app.use('/api/', routerMain);
app.listen(PORT, () => {
consola.success(`⚡️[server]: Server is running at http://localhost:${PORT}`);
});
async function main() {
await loaders(app);
app.listen(PORT, () => {
consola.success(`⚡️[server]: Server is running at http://localhost:${PORT}`);
});
}
main()

5
src/loaders/database.ts Normal file
View File

@ -0,0 +1,5 @@
import {createConnection} from "typeorm";
export default async ()=> {
return await createConnection();
};

14
src/loaders/express.ts Normal file
View File

@ -0,0 +1,14 @@
import {Application} from "express";
import bodyParser from 'body-parser';
import cors from 'cors';
export default async (app: Application) => {
app.get('/status', (req, res) => res.status(200).end());
app.enable('trust proxy');
app.use(cors());
// app.use(bodyParser.urlencoded({ extended: false }));
// more middlewares
return app;
};

View File

@ -1,14 +1,11 @@
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import expressLoader from "./express";
import routeLoader from "./routes";
import databaseLoader from "./database";
import {Application} from "express";
export default async (app) => {
app.get('/status', (req, res) => res.status(200).end());
app.enable('trust proxy');
app.use(cors());
// app.use(bodyParser.urlencoded({ extended: false }));
// more middlewares
return app;
export default async (app: Application) => {
await expressLoader(app);
await routeLoader(app);
databaseLoader();
return app;
};

7
src/loaders/routes.ts Normal file
View File

@ -0,0 +1,7 @@
import {Application} from "express";
import routerMain from "../routes/index";
export default async (app: Application) => {
app.use('/api/', routerMain);
return app;
};