diff --git a/apis/product/app.js b/apis/product/app.js index 486fea2..e43b106 100644 --- a/apis/product/app.js +++ b/apis/product/app.js @@ -1,22 +1,18 @@ import express from 'express'; import cookieParser from 'cookie-parser'; import logger from 'morgan'; -import dotenv from 'dotenv'; import cors from 'cors'; -import db from './db'; import indexRouter from './routes/index'; import productRouter from './routes/product'; import infoRouter from './routes/info'; import secretRouter from './routes/secret'; import etagGenerator from './routes/middleware/etag-generator'; - -dotenv.config(); -db().catch(() => { - process.exit(); -}); +import { dbConnect } from './config'; const app = express(); +app.use(dbConnect()); + app.use(cors()); app.use(logger('dev')); app.use(express.json()); @@ -32,9 +28,8 @@ app.use('/secrets', secretRouter); // eslint-disable-next-line no-unused-vars app.use((err, req, res, next) => { - const message = err.message.split('|')[0].trim(); res.status(err.status || 500); - res.json(message); + res.json(err.message); }); module.exports = app; diff --git a/apis/product/bin/www.js b/apis/product/bin/www.js index 74a4b17..73ca7a4 100755 --- a/apis/product/bin/www.js +++ b/apis/product/bin/www.js @@ -4,6 +4,7 @@ * Module dependencies. */ +var dotenv = require('dotenv').config(); var app = require('../app'); var debug = require('debug')('template:server'); var http = require('http'); diff --git a/apis/product/config/index.js b/apis/product/config/index.js index d290792..5401062 100644 --- a/apis/product/config/index.js +++ b/apis/product/config/index.js @@ -1,3 +1,26 @@ +import mongoose from 'mongoose'; + +export const dbConnect = () => { + const options = { + useNewUrlParser: true, + useUnifiedTopology: true, + keepAlive: false, + }; + let errorMessage; + mongoose.connect(`${process.env.MONGO_URL}`, options, (err) => { + if (err) { + errorMessage = err.toString(); + } + }); + return (req, res, next) => { + if (errorMessage) { + next({ status: 500, message: errorMessage }); + } else { + next(); + } + }; +}; + export const mongoosasticSettings = (process.env.NODE_ENV === 'test') ? {} : { hosts: process.env.ELASTICSEARCH, port: process.env.ESPORT, diff --git a/apis/product/db/index.js b/apis/product/db/index.js deleted file mode 100644 index f257748..0000000 --- a/apis/product/db/index.js +++ /dev/null @@ -1,19 +0,0 @@ -import mongoose from 'mongoose'; - -export default async () => { - const options = { - useNewUrlParser: true, - useUnifiedTopology: true, - }; - await mongoose.connect(`${process.env.MONGO_URL}`, options, (err) => { - if (err) { - throw new Error(err); - } - }); - - - const db = mongoose.connection; - db.on('err', () => { - throw new Error('connection Error'); - }); -}; diff --git a/apis/product/services/elasticsearch/statics.js b/apis/product/services/elasticsearch/statics.js deleted file mode 100644 index e69de29..0000000 diff --git a/apis/product/services/index.js b/apis/product/services/index.js deleted file mode 100644 index e69de29..0000000