Skip to content

Commit

Permalink
feat: add mongoose conf
Browse files Browse the repository at this point in the history
  • Loading branch information
MriLiuJY committed Jul 1, 2019
1 parent b9c13ed commit 1e9336c
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
155 changes: 155 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"eslint-loader": "^2.1.2",
"eslint-plugin-import": "^2.17.3",
"jest": "^24.8.0",
"mongoose": "^5.6.2",
"uglifyjs-webpack-plugin": "^2.1.3",
"webpack": "^4.34.0",
"webpack-dev-middleware": "^3.7.0",
Expand Down
24 changes: 24 additions & 0 deletions server/database/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require("mongoose");
const db = "mongodb://localhost/test";

mongoose.Promise = global.Promise;

exports.connect = () => {
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}

mongoose.connect(db);

mongoose.connection.on("disonnected", () => {
mongoose.connect(db);
});

mongoose.connection.on("err", err => {
mongoose.connect(db);
});

mongoose.connection.on("open", () => {
console.log("Mongoodb connect done!");
});
};
5 changes: 5 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ const hotMiddleware = require("webpack-hot-middleware");
const config = require("../config/webpack.dev.config");
const bodyParser = require("body-parser");
const session = require('express-session');
const { connect } = require('./database/init');

const app = express();
app.use(express.static("./"));
const complier = webpack(config);

(async() => {
await connect();
})();

app.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
Expand Down

0 comments on commit 1e9336c

Please sign in to comment.