how to require some libraries only for the server
[issue link]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?