|
|
@@ -2,12 +2,32 @@ import * as path from "path";
|
|
|
import cookieParser from "cookie-parser";
|
|
|
import * as bodyParser from "body-parser";
|
|
|
import logger from "morgan";
|
|
|
-import express from "express";
|
|
|
+import express, { Application } from "express";
|
|
|
import rootRouter from "./routes/root";
|
|
|
import recipeRouter from "./recipes/routes";
|
|
|
import nano from 'nano';
|
|
|
import dotenv from 'dotenv';
|
|
|
import MongoClient from 'mongodb';
|
|
|
+import { Response } from 'express';
|
|
|
+import mongoService from './mongo';
|
|
|
+import { RecipeDB } from './recipes/model';
|
|
|
+
|
|
|
+namespace Application {
|
|
|
+ locals: {
|
|
|
+ db: MongoClient.MongoClient
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+declare global {
|
|
|
+ namespace Express {
|
|
|
+ interface Response {
|
|
|
+ app: Application,
|
|
|
+ locals: {
|
|
|
+ savedData: RecipeDB
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
class App {
|
|
|
public app: express.Application;
|
|
|
@@ -16,16 +36,15 @@ class App {
|
|
|
this.dotENV();
|
|
|
this.app = express();
|
|
|
this.middleware();
|
|
|
- this.routes();
|
|
|
- this.launchConf();
|
|
|
+ this.launchApp();
|
|
|
}
|
|
|
|
|
|
private dotENV(): void {
|
|
|
dotenv.config();
|
|
|
}
|
|
|
|
|
|
- private routes(): void {
|
|
|
- this.app.use("/recipes", recipeRouter);
|
|
|
+ private routes(db: MongoClient.Db): void {
|
|
|
+ this.app.use("/recipes", new recipeRouter(db).router);
|
|
|
this.app.use("/", rootRouter);
|
|
|
}
|
|
|
|
|
|
@@ -39,13 +58,13 @@ class App {
|
|
|
this.app.use(express.static(path.join(__dirname, "public")));
|
|
|
}
|
|
|
|
|
|
- private launchMongoDb(): Promise<MongoClient.MongoClient> {
|
|
|
- return MongoClient.connect('mongodb://127.0.0.1:27017/api');
|
|
|
+ private connectDB(): Promise<MongoClient.Db> {
|
|
|
+ return MongoClient.connect('mongodb://127.0.0.1:27017').then(dbClient => dbClient.db('ktchn'));
|
|
|
}
|
|
|
|
|
|
- private launchConf() {
|
|
|
- this.launchMongoDb().then(dbClient => {
|
|
|
- this.app.locals.db = dbClient;
|
|
|
+ private launchApp() {
|
|
|
+ this.connectDB().then(db => {
|
|
|
+ this.routes(db);
|
|
|
const port = this.app.get("port");
|
|
|
this.app.listen(port, () => {
|
|
|
console.log(
|