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 );
});
req.headers
? – Celso Agrapost
rathen thanget
. – Celso Agrares.setHeader('Content-Type', 'text/html');
(or maybe could beapplication/json
). Then, you can send this response (res.send(....)
) – Celso Agra