2017-02-09 35 views
0

我有一個安裝程序,我試圖從控制檯註銷我的請求和從app.get('*',函數(req,我是新來表達和節點,並發現我的日誌出來undefined。快速請求和響應正文始終是一個空對象和Content-Type始終未定義

我已經閱讀了至少10 stackoverflow上的帖子,我的中間件解析器需要來路由之前和剛過應用程序的初始化。這是我在這裏做什麼。 任何人都知道是怎麼回事。

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

上面一行將返回REQ體區= {}和資源型=未定義

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); 
    }); 
+0

只是爲了理解......你是否試圖從響應變量中獲取內容類型!?請求變量呢,在'req.headers'中呢? –

+0

是的我試圖從響應中獲取內容類型。 req.headers給我提供數據。但想知道爲什麼我無法從請求中獲得任何身體數據。 –

+0

你無法從get方法中獲取任何內容。嘗試將此更改爲'發佈'比'獲取'更好。 –

回答

0

也許這可以解決您的問題:

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

更改GET方法後或放方法。