In case you need to require some libraries only for the server, you can use the process.server variable set to true when webpack is creating the server.bundle.js file.
I don't know what exactly I should do.
For example I want to take data from Mongodb with mongoose into asyncData
. How can i make mongoose server-only?
~/mongo/mongo-test.js
import mongoose from 'mongoose'
const user = new mongoose.Schema({
// some definition
})
const userModel = mongoose.model('user',user)
export function findSomeone(){
userModel.find({
// find datas
} ,callback)
}
~/router/index.vue
<template>
<div></div>
</template>
<script>
import mongoTest from '~/mongo/mongo-test.js'
export default {
asyncData: (context) => { if (context.isServer) { mongoTest.findSomeone() } },
}
</script>
<style>
</style>
Or should i use nuxt as a middleware?
<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c2133">#c2133</a>)</em></sub></div>