Skip to content

Commit

Permalink
fix: fixed 500 error when db connection failed
Browse files Browse the repository at this point in the history
- DB๊ฐ€ ์ ‘์†๋˜์ง€ ์•Š์„ ๋•Œ ์„œ๋ฒ„๋ฅผ ์ข…๋ฃŒํ•˜์—ฌ api์—ฐ๊ฒฐ ์‹œ ์‹คํŒจ ์›์ธ์„ ๋ชจ๋ฅด๋Š”
๊ฒƒ๋ณด๋‹ค, 500 ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์™œ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋˜๋Š”์ง€ ๋ช…ํ™•ํžˆ ํ‘œ์‹œํ•˜์—ฌ
ํด๋ผ์ด์–ธํŠธ๋‚ด์—์„œ ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€๊ฒฝ
- ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ ์ œ๊ฑฐ
  • Loading branch information
kgpyo committed Dec 18, 2019
1 parent 3107ae6 commit 873f3d1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
13 changes: 4 additions & 9 deletions apis/product/app.js
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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;
1 change: 1 addition & 0 deletions apis/product/bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 23 additions & 0 deletions apis/product/config/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
19 changes: 0 additions & 19 deletions apis/product/db/index.js

This file was deleted.

Empty file.
Empty file removed apis/product/services/index.js
Empty file.

0 comments on commit 873f3d1

Please sign in to comment.