jonniebigodes
jonniebigodes

Reputation: 137

Express route in controller not resolved

I'm using express 4.16.3 and trying to make sense of why one request to a controller works and a request doesn't.

in my server.js i've got the following. There's no semi-colons because used prettier beforehand.

import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import PriceCheckerController from './controllers/PriceChecker'
import PersonalLibraryController from './controllers/PersonalLibrary'

const app = express()
app.set('port', process.env.PORT || 5000)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors())
// routes definition
app.use('/api/books',PersonalLibraryController) // this does not
app.use('/api/stock-prices', PriceCheckerController) // this works
//
app.listen(app.get('port'), error => {
  if (error) {
   logger.error(`error fcc-isqa:${error}`)
  } else {
   logger.info(`fcc-isqa is running on port ${app.get('port')}`)
  }
})
export default app

In PriceCheckerController i've implemented it like so.

import 'babel-polyfill' // mocha required
import express from 'express'
import logger from '../logger'

const PriceCheckerController = express.Router()
PriceCheckerController.use((req, res, next) => {
  logger.info(
    `date=>${new Date()}\n method=>${req.method}nsender:${req.ip}`
  )
})
PriceCheckerController.get('/', async (req, res) => {
   return res.status(200).json({message:'soon'})
})
export default PriceCheckerController

In PersonalLibraryController i've implemented it like so

import 'babel-polyfill'
import express from 'express'
import logger from '../logger'


const PersonalLibraryController = express.Router()
PersonalLibraryController.use((req,res,next)=>{
   logger.info(
    `library date=>${
      new Date()}method=>${req.method}url=>${req.baseUrl}${req.path}`
    )
})
PersonalLibraryController.route('/test')
.get(async (req, res) => {
    return res.status(200).json({message: 'get soon'})
})
.post(async (req,res)=>{
    return res.status(200).json({message: 'post soon'})
})
export default PersonalLibraryController

A request to /api/stock-prices returns ok with message soon. A request to /api/books/test is logged by the middleware but a response is not sent back. It eventually gives a timeout

Can anyone give me any insights/ help in understanding what is the problem and how to fix it? Thanks in advance.

Upvotes: 0

Views: 1227

Answers (1)

TommyBs
TommyBs

Reputation: 9646

Your middleware functions need to call next() to carry on the route execution.

e.g

PriceCheckerController.use((req, res, next) => {
  logger.info(
    `date=>${new Date()}\n method=>${req.method}nsender:${req.ip}`
  )
  next();
})

https://expressjs.com/en/guide/using-middleware.html

"If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging."

Upvotes: 2

Related Questions