-2

// app.js code

var express = require("express");
var app = express();
var bodyParser = require("body-parser");

app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}))

app.get("/",function(req,res){
    res.render("home");
});

app.get("/friends",function(req,res){
    var friends = ["tony","miranda","pierre","lilly"]
    res.render("friends",{friends:friends});
});

app.post("/addfriend",function(req,res){
    console.log(req.body);

    res.send("You have reached the post route")
});

app.listen(3000,function(){
    console.log("Server Started!");

});

// friends.ejs code

<h1>Friends</h1>

<% friends.forEach(function(friend){ %>
    <li>I Have a Friend = <%= friend %></li>
<% }) %>

<form action="/addfriend" method="POST">

    <input type="text" placeholder="name" name="newfriend">
    <button>Add Me</button>
</form>

The response I get:

Server Started!
{}
{ newfriend: 'ahnaf' }

please review the code above and I created an express app whenever I request for one response I get two response.In the response section mentioned here the extra response is the empty object.Please help me out

  • What action do you take in the browser that generates the empty object in the node.js console? Do you have any Javascript in the web page that is participating in sending your form to the server? – jfriend00 May 25 '20 at 15:14
  • yesss I send a single input to the server – Ahnaf Ahamed May 25 '20 at 16:05
  • Please read the question I asked in my previous and answer it. What specific action do you do in the browser that causes this? Press a button? Type a URL? Hit Return? Also, is there Javascript active in the page? If so, show it to use please. – jfriend00 May 25 '20 at 18:20
  • try Commenting line res.send("You have reached the post route") – Jin Thakur May 25 '20 at 19:01
  • Please check this code i think thats what you want. – Jin Thakur May 26 '20 at 14:29
  • code link https://github.com/cemtorun/learning_web_dev/blob/master/Section%2026:%20Intermediate%20Express/friendsProject/app.js – Jin Thakur May 26 '20 at 14:29
  • check your code you should put ; after every line of code. you are logging request where is code for your /addfriend code. When – Jin Thakur May 26 '20 at 14:29
  • @jfriend00 there is no javascript required to post. You post directly by app.post("/addfriend", function(req,res){ // makes it so we can see what the post req is sending to server var newFriend = req.body.newfriend; // body parser needs to see req in js form from express // body parser parsed express obj to js object friends.push(newFriend); res.redirect("/friends"); //after running add friends, it also runs /friends to update the list }); – Jin Thakur May 26 '20 at 14:32

1 Answers1

0

Please check this code https://github.com/cemtorun/learning_web_dev/blob/master/Section%2026:%20Intermediate%20Express/friendsProject/app.js

check your code you should put ; after every line of code. you are logging request where is code for your /addfriend code. When you are posting something to the page. You are refreshing the whole page. So one is getting a new friend /addfriend does show you body { newfriend: 'ahnaf' }

then you have this line res.send .comment this and write it like this

you can write a middleware to intercept data written to the response. Make sure you disable app.compress().

function logResponseBody(req, res, next) {
  var oldWrite = res.write,
      oldEnd = res.end;

  var chunks = [];

  res.write = function (chunk) {
    chunks.push(chunk);

    oldWrite.apply(res, arguments);
  };

  res.end = function (chunk) {
    if (chunk)
      chunks.push(chunk);

    var body = Buffer.concat(chunks).toString('utf8');
    console.log(req.path, body);

    oldEnd.apply(res, arguments);
  };

  next();
}

then use like this.

app.use(logResponseBody);
Jin Thakur
  • 2,711
  • 18
  • 15
  • `app.post("/addfriend", ...)` just does a `res.send()`. It, by itself does not cause a double page refresh. That is likely caused by something else in the page that the OP is not showing us. No middleware like this is needed to solve this problem. Not sure where you got this idea from. – jfriend00 May 25 '20 at 18:21
  • @jfriend00 I don't know why re you so confident?.https://stackoverflow.com/questions/19215042/express-logging-response-body – Jin Thakur May 25 '20 at 19:00
  • Yeah, but what does that have to do with this problem in the slightest? What evidence do you see in this question that it has anything at all to do with this answer? – jfriend00 May 25 '20 at 19:29
  • @jfriend00 he is saying he is getting two response. in face he is not getting two response. he is logging request req.body which is that json. – Jin Thakur May 26 '20 at 14:20
  • @jfriend00 this is what he is trying to codehttps://github.com/cemtorun/learning_web_dev/blob/master/Section%2026:%20Intermediate%20Express/friendsProject/friends.ejs – Jin Thakur May 26 '20 at 14:31