最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to print the response body (or the request body when POST) in Node.js using Express? - Stack Overflow

programmeradmin0浏览0评论
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');


mongoose.connect("mongodb://localhost/placement-process");

var app = express();

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

var router = require('./router');
app.use('/api', router);

router.get('/panies', function(req, res){
    console.log(res.body);
});


app.listen(3000);

I am making GET requests to /api/panies using POSTMAN for Chrome browser. But I am not getting any output in my console. What am I doing wrong? Or how would you do it?

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');


mongoose.connect("mongodb://localhost/placement-process");

var app = express();

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

var router = require('./router');
app.use('/api', router);

router.get('/panies', function(req, res){
    console.log(res.body);
});


app.listen(3000);

I am making GET requests to /api/panies using POSTMAN for Chrome browser. But I am not getting any output in my console. What am I doing wrong? Or how would you do it?

Share Improve this question asked Mar 25, 2017 at 13:08 Uddipta_DeuriUddipta_Deuri 731 gold badge1 silver badge8 bronze badges 4
  • What do you expect req.body to be for GET requests? – robertklep Commented Mar 25, 2017 at 13:50
  • @robertklep I want to print the JSON object that is returned from my GET request. For example, [{ "_id" : ---some value----, "car" : "Porsche", "model": "Cayman" } ] – Uddipta_Deuri Commented Mar 25, 2017 at 14:30
  • That's not something that Express provides out-of-the-box, you're going to need a specific middleware to implement that (like express-interceptor, express-mung, express-buffer-response). – robertklep Commented Mar 25, 2017 at 14:34
  • oh ok. thank you. I was busy doing it another way. I actually needed to validate, and I only discovered the word validations a few hours after. – Uddipta_Deuri Commented Mar 28, 2017 at 14:10
Add a ment  | 

1 Answer 1

Reset to default 5

In your code you are doing

router.get('/panies', function(req, res){
  console.log(res.body);
});

While it should be

router.get('/panies', function(req, res){
  console.log(req.body); // req, not res altough it shouldn't be set for GET
});

You wanted to print Request body not Response (which is unset, of course). Let me know if it solved your issue.

[edit]

Since you ask to me how would I do it, probably you should note that using console.log is not how I'd log something in production. Rather I'd use a logging library like bunyan, which is extensible and features a json-only logging. I like their vision that logs should be though for machines and not for humans, by the way they also provide a binary tool to pretty print JSON in a more human friendly format. It's extensible and you can find a lot of plugins (referred to as "streams") that log also to log aggregators and third party services should you need that kind of feature.

Uh, and of course there are a lot of other valid alternatives out there.

[update as per OP ment]

I think I now understand what you're trying to achieve. You should have a look at the response actually after it has been generated. Where you are trying to print something you didn't even touch the response object, so it should not be modified (unless you have some middleware doing that for you -e.g. putting some header-). Refer to a library such as on-finished and you'll get the response object right before to send it to the client.

You don't explicitly state your express or node version so I can only guess, but give it a try.

You could also go with plain node.js APIs.

Also you might just put another middleware right after your handler and pass the request next to it with a response set and just log (express router is stack-based so unless you terminate the request you are able to pass in several handlers).

BTW I'd just try our the Node.js API or the library I posted to you.

Let me know if I've understood what you meant to do.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论