|
@@ -7,55 +7,58 @@ import rootRouter from "./routes/root";
|
|
|
import recipeRouter from "./recipes/routes";
|
|
import recipeRouter from "./recipes/routes";
|
|
|
import nano from 'nano';
|
|
import nano from 'nano';
|
|
|
import dotenv from 'dotenv';
|
|
import dotenv from 'dotenv';
|
|
|
|
|
+import MongoClient from 'mongodb';
|
|
|
|
|
|
|
|
class App {
|
|
class App {
|
|
|
- public express: express.Application;
|
|
|
|
|
- //public db: nano.DatabaseScope;
|
|
|
|
|
|
|
+ public app: express.Application;
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
this.dotENV();
|
|
this.dotENV();
|
|
|
- this.express = express();
|
|
|
|
|
|
|
+ this.app = express();
|
|
|
this.middleware();
|
|
this.middleware();
|
|
|
this.routes();
|
|
this.routes();
|
|
|
this.launchConf();
|
|
this.launchConf();
|
|
|
- //this.db = this.couchDB();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private dotENV(): void {
|
|
private dotENV(): void {
|
|
|
dotenv.config();
|
|
dotenv.config();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private couchDB(): nano.DatabaseScope {
|
|
|
|
|
- return nano(process.env.COUCHDB_URL || 'http://localhost:5984/').db;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
private routes(): void {
|
|
private routes(): void {
|
|
|
- this.express.use("/recipes", recipeRouter);
|
|
|
|
|
- this.express.use("/", rootRouter);
|
|
|
|
|
|
|
+ this.app.use("/recipes", recipeRouter);
|
|
|
|
|
+ this.app.use("/", rootRouter);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private middleware(): void {
|
|
private middleware(): void {
|
|
|
- this.express.set("port", process.env.PORT || 3000);
|
|
|
|
|
- this.express.use(logger("dev"));
|
|
|
|
|
- this.express.use(express.json());
|
|
|
|
|
- this.express.use(bodyParser.json());
|
|
|
|
|
- this.express.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
|
- this.express.use(cookieParser());
|
|
|
|
|
- this.express.use(express.static(path.join(__dirname, "public")));
|
|
|
|
|
|
|
+ this.app.set("port", process.env.PORT || 3000);
|
|
|
|
|
+ this.app.use(logger("dev"));
|
|
|
|
|
+ this.app.use(express.json());
|
|
|
|
|
+ this.app.use(bodyParser.json());
|
|
|
|
|
+ this.app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
|
+ this.app.use(cookieParser());
|
|
|
|
|
+ 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 launchConf() {
|
|
private launchConf() {
|
|
|
- const port = this.express.get("port");
|
|
|
|
|
- console.log('port', port);
|
|
|
|
|
- this.express.listen(port, () => {
|
|
|
|
|
- console.log(
|
|
|
|
|
- ("App is runnnning at http://localhost:%d in %s mode"),
|
|
|
|
|
- port,
|
|
|
|
|
- this.express.get("env")
|
|
|
|
|
- );
|
|
|
|
|
- console.log("Press CTRL-C to stop");
|
|
|
|
|
|
|
+ this.launchMongoDb().then(dbClient => {
|
|
|
|
|
+ this.app.locals.db = dbClient;
|
|
|
|
|
+ const port = this.app.get("port");
|
|
|
|
|
+ this.app.listen(port, () => {
|
|
|
|
|
+ console.log(
|
|
|
|
|
+ ("App is runnnning at http://localhost:%d in %s mode"),
|
|
|
|
|
+ port,
|
|
|
|
|
+ this.app.get("env")
|
|
|
|
|
+ );
|
|
|
|
|
+ console.log("Press CTRL-C to stop");
|
|
|
|
|
+ });
|
|
|
|
|
+ }).catch(error => {
|
|
|
|
|
+ console.log(`Error loading database: ${error}`)
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export default new App().express;
|
|
|
|
|
|
|
+export default new App().app;
|