2017-02-09 9 views
0

私はコンソールから私の要求と応答をログアウトするための設定をapp.get( '*'、function(req、明示的な要求と応答のボディは常に空のオブジェクトであり、Content-Typeは常に定義されていません

私は、ミドルウェアパーサーがルートとその前に来る必要があるstackoverflowで少なくとも10の記事を読んだことがあります。上記の行は、REQを返しますちょうどアプリの初期化。私はここでやっているの後。 誰もがここで何が起こっているか知っている。

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

-body = {}とRES型=未定義

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

ちょっと理解する...あなたはレスポンス変数からContent-typeを取得しようとしていますか? 'req.headers'のリクエスト変数はどうですか? –

+0

はい応答からContent-typeを取得しようとしています。 req.headersは私にデータを与えます。なぜ私は要求から身体のデータを得ることができないのだろうと思っています。 –

+0

getメソッドからボディを取得することはできません。 'get'よりも' post''に変更してみてください。 –

答えて

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

変更ポストまたはputメソッドにGETメソッド。

関連する問題