1

How to access configuration declared in privateRuntimeConfig in Nuxt.config.js file in serverMiddleware?

$config and context are not available in serverMiddleware.

I am using serverMiddleware in Nuxtjs to write api.

Its getting called however I am trying to pass some configuration from privateRuntimeConfig in Nuxt.config.js file.

const bodyParser = require('body-parser')
const app = require('express')()
const { uuid } = require('vue-uuid')
const productsModule = require('../lib/bal/products')

app.use(bodyParser.json())
app.post('/create', (req, res) => {
    console.log('Config:' + String(req))
    const result = productsModule.createProduct(this.$config, req.body.name, 'Trial product', '', 10, false, uuid.v1)
    if (result === undefined) {
        res.status(500).json({ error: 'Failed to create product. Try again!' })
        return
    }
    console.log(result)
    res.status(200).json(result)
})

module.exports = app
user859375
  • 3,529
  • 5
  • 21
  • 22

1 Answers1

0

Yes you are right, since serverMiddleware only runs at service-side you can't use this.$config or context.$config. What i did is, if it is a static data, i use environment variables to call the data.

.env file

APP_USERNAME=M457ERCH1EF

serverMiddleware file i.e xxx.js

....
const username = process.env.APP_USERNAME
....