Implemented mongoose into the server.ts

This commit is contained in:
Mathias Wagner 2023-01-14 18:44:23 +01:00
parent 5e40948747
commit 13b23a2cbd
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,8 +1,18 @@
import express, {Application} from 'express'; import express, {Application} from 'express';
import mongoose, {CallbackError} from "mongoose";
const MONGOOSE_STRING = process.env.MONGOOSE_STRING || 'mongodb://localhost:27017';
const app: Application = express(); const app: Application = express();
const port: number = parseInt(process.env.SERVER_PORT || '8025'); const port: number = parseInt(process.env.SERVER_PORT || '8025');
const isDevelopment: boolean = process.env.NODE_ENV !== 'production'; const isDevelopment: boolean = process.env.NODE_ENV !== 'production';
mongoose.set("strictQuery", false);
mongoose.connect(MONGOOSE_STRING, (error: CallbackError) => {
if (error) throw new Error(`Could not connect to database: ${error.message}`);
console.log(`Successfully connected to the database @${MONGOOSE_STRING.split("://")[1].split("/")[0]}`);
run();
});
app.listen(port, () => console.log(`LicenseAPI ${isDevelopment ? "development" : "production"} server started under port ${port}`)); const run = () =>
app.listen(port, () => console.log(`LicenseAPI ${isDevelopment ? "development" : "production"} server started under port ${port}`));