diff --git a/app.js b/app.js index df799d0..853fbe7 100644 --- a/app.js +++ b/app.js @@ -4,20 +4,20 @@ const jwt = require('jsonwebtoken') const { v1: uuidv1, NIL } = require('uuid'); const reqPromise = require('request-promise') const mongoose = require('mongoose') -const dotevn = require('dotenv') -dotevn.config() -const uri = `mongodb+srv://${process.env.DB_username}:${process.env.DB_password}@cluster0.f2gkp.mongodb.net/BelayChainNode1?retryWrites=true&w=majority` +const Blockchain = require('./src/Blockchain'); +const connectDatabase = require('./database/database') + const { Schema } = mongoose -// const port = process.env.PORT || 9000 const port = process.argv[2] const corsOptions = { origin: '*', methods: 'GET,POST,PUT,PATCH,DELETE' } -const Blockchain = require('./src/Blockchain'); -const bodyParser = require('body-parser'); const app = express(); +app.use(cors(corsOptions)) +app.use(express.json()) + const blockSchema = new Schema({ index: Number, timestamp: Number, @@ -35,14 +35,6 @@ const Block = mongoose.model('Block', blockSchema) const Node = mongoose.model('Node', nodeSchema) -app.use(cors(corsOptions)) -app.use(express.json()) - -mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true}) - .then(() => console.log("MONGODB CONNECTED")) - .catch(console.error) - - let BelayChain = [] const loadBelayChain = () => { diff --git a/database/database.js b/database/database.js new file mode 100644 index 0000000..7b48f9c --- /dev/null +++ b/database/database.js @@ -0,0 +1,8 @@ +const dotevn = require('dotenv') +dotevn.config() +const uri = `mongodb+srv://${process.env.DB_username}:${process.env.DB_password}@cluster0.f2gkp.mongodb.net/BelayChainNode1?retryWrites=true&w=majority` +const mongoose = require('mongoose') + +module.exports = mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true}) + .then(() => console.log("MONGODB CONNECTED")) + .catch(console.error) \ No newline at end of file