Express dynamic route is not working on production

I want to send data from client to server so I am using express dynamic routes. I use it on my localhost and its working very well without an error. When I deploy it to heroku it says TypeError: Cannot read property 'twittername' of undefined

Here is my codes:
server/routes/api/twitterName.js

const express = require('express')
const router = express.Router()

router.get('/:twittername', (req, res) => {
  res.send(req.params.twittername)
})

module.exports = router

server/routes/api/tweets.js

const express = require('express')
const T = require('../../../src/lib/twitter')
const twitterName = require('./twitterName')

const router = express.Router()

router.get('/', (req, res) => {
  console.log(twitterName.stack[0].params.twittername)
  T.get(
    'search/tweets',
    { from: twitterName.stack[0].params.twittername, count: 5 },
    function (err, data, response) {
      if (err) {
        return res.status(400).json('Oops! Something went wrong.')
      }
      res.status(200).send(data)
    }
  )
})

module.exports = router

and server/index.js

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const path = require('path')

const app = express()
app.use(bodyParser.json())
app.use(cors())

const tweets = require('./routes/api/tweets')
const twitterName = require('./routes/api/twitterName')

app.use('/api/tweets', tweets)
app.use('/twittername/', twitterName)

if (process.env.NODE_ENV === 'production') {
  app.use(express.static(__dirname + '/public/'))
  app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html'))
}

const port = process.env.PORT || 8081

app.listen(port, () => console.log(`Server started on port ${port}`))

Source: Ask Javascript Questions

LEAVE A COMMENT