我想将数据从客户端发送到服务器,所以我使用快速动态路由。我在我的本地主机上使用它并且它工作得很好,没有错误。当我将它部署到heroku时,它说TypeError: Cannot read property 'twittername' of undefined
这是我的代码: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
服务器/路由/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
和服务器/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}`))