0
votes

I have a setup where i'm trying to console log out my request and response from app.get('*', function (req, res, next) {. I'm new to express and node and am finding out that my logs are coming out undefined.

I have read at least 10 posts on stackoverflow that my middleware parsers need to come before the routes and just after the app initialization. Which is what I'm doing here. Anyone know what is going on here.

  console.log("url = ", req.url, "  req-body  = ", req.body,   "res-type  = ", res.get('Content-Type'));

The above line will return req-body = {} and res-type = undefined

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

    const host = 'localhost';
    const port = process.env.PORT || 3002;

    const app = express()
    app.use(bodyParser.json());
    app.use(bodyParser.text());
    app.use(bodyParser.urlencoded({ extended: true }));

    const indexPath = path.join(__dirname, '../index.html')
    const publicPath = express.static(path.join(__dirname, 'public'))

    // DEV SERVER WEBPACK API
    if (process.env.NODE_ENV !== 'production') {
      const webpack = require('webpack')
      const webpackDevMiddleware = require('webpack-dev-middleware')
      const webpackHotMiddleware = require('webpack-hot-middleware')
      const config = require('../webpack/webpack.dev.config.js')
      const compiler = webpack(config)

      // WEBPACK MIDDLEEWARE
      app.use(webpackHotMiddleware(compiler))
      app.use(webpackDevMiddleware(compiler, {
        noInfo: true,
        publicPath: config.output.publicPath,
        stats: { colors: true },
        historyApiFallback: true
      }))
    }

    // MIDDLEWARE FOR PRODUCTION TO SERVE FILES DIRECTLY FROM PUBLIC.
    app.use('/public', publicPath)

    // REQUEST CALLS.
    app.get('*', function (req, res, next) {
      console.log("url = ", req.url, "  req-body  = ", req.body,   "res-type  = ", res.get('Content-Type'));
      res.sendFile(indexPath);
    })

   app.listen(port, host, () =>{
     console.log("server ready on port  :", port, " and on host : ", host );
   });
1
Just to understand... are you trying to get the Content-type from the response variable!? What about the request variable, in the req.headers?Celso Agra
Yes I'm trying to get the Content-type from the response. req.headers gives me data. but wondering why I can't get any body data from request.me-me
You can't get any body from get method. Try to change this to post rathen than get.Celso Agra
Ah didn't know that thanks for clearing that up. What about res.get('Content-Type') on a get method ?me-me
Cool! You should try to get the Content type from 'request.headers'. But you are trying to pass a different Content-Type, you can set this res.setHeader('Content-Type', 'text/html'); (or maybe could be application/json). Then, you can send this response (res.send(....))Celso Agra

1 Answers

0
votes

Maybe this could solve your issue:

app.post('*', function (req, res, next) {
      console.log("url = ", req.url, "  req-body  = ", req.body,   "res-type  = ", res.get('Content-Type'));
      res.sendFile(indexPath);
    })

Change the get method to a post or put method.