2017-02-09 3 views
0

Ich habe ein Setup, wo ich versuche, meine Anfrage und Antwort von mir abzumelden. App.get ('*', Funktion (req, res, next) {. Ich bin neu auszudrücken und Knoten und bin dabei, herauszufinden, dass meine Protokolle undefined.Express-Anfrage und Response Body ist immer ein leeres Objekt und Content-Type ist immer undefiniert

Ich habe mindestens 10 Beiträge auf Stackoverflow gelesen, dass meine Middleware Parser vor den Routen und kommen müssen kurz nach der App-Initialisierung. Das ist, was ich hier tue. Wer weiß, was hier vor sich geht.

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

die obige Zeile wird erf zurückkehren -body = {} und 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); 
    }); 
+0

Einfach zu verstehen ... versuchen Sie, den Inhaltstyp aus der Antwortvariablen zu bekommen !? Was ist mit der Anfrage Variable, in den 'req.headers'? –

+0

Ja, ich versuche, den Inhaltstyp aus der Antwort zu erhalten. req.headers gibt mir Daten. aber ich frage mich, warum ich keine Körperdaten von der Anfrage bekommen kann. –

+0

Sie können keinen Körper von Get-Methode bekommen. Versuchen Sie, dies in 'post' rathen als' get' zu ändern. –

Antwort

0

Vielleicht könnte dies Ihr Problem lösen:

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

Ändern Sie die get-Methode zu einem Post oder setzen Methode.