nuxt-community/express-template

MongoDB

Closed this issue · 0 comments

How i can add connection with mongoose in this project?
I tried to do it, but I can not understand how to make both connections.
I'll be glad if you help me with this

import { Nuxt, Builder } from 'nuxt'
import express from 'express'
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const mongoose = require('mongoose')
const config = require('./config/config')
mongoose.Promise = global.Promise

const app = express()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 3000

app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors())
app.use(require('./routes/users'))

mongoose.connect(config.dbURL)

let nuxtConfig = require('../nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')

const nuxt = new Nuxt(nuxtConfig)

if (config.dev) {
  const builder = new Builder(nuxt)
  builder.build()
}
app.set('port', port)
app.use(nuxt.render)

mongoose.connection
  .once('open', () => {
    console.log(`Mongoose - successful connection ...`)
    app.listen(config.port,
      () => console.log(`Server start on port ${config.port} ...`))
  })
  .on('error', error => console.warn(error))

config.js

module.exports = {
  port: 8081,
  dbURL: 'mongodb://localhost/data'
}

This question is available on Nuxt.js community (#c94)