简体   繁体   中英

Sending POST request body to web page with NODEJS

I have the following setup: 1) NodeJs server 2) External Application

I would like to receive a POST request from the External Application and send the body of the POST request to a web page for visualization.

So far I can receive successfully the POST request and print it in the server console. I'm using express and body-parser to handle the server and the message body.

Here is the code:

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

app.use(myParser.json({extended : true}));
app.post("/", function(request, response) {
console.log(request.body); 
});

app.listen(3000);

On the POST event I just print the body message. I'm not even sending the response back since I know that it has no use for the External Application. Now, for the second part, the visualization on the web page, I need a "get" event in my app, provided by the browser connecting to the server.

So I modified the server code as follow:

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

app.use(myParser.json({extended : true}));
app.post("/", function(request, response) {
console.log(request.body); 
app.get("/index.html",function(req,res){
res.end(request.body);});
});

app.listen(3000);

On server start I don't get any error, but if I open the browser on the index.html resource I get this error:

TypeError: First argument must be a string or Buffer at ServerResponse.end (_http_outgoing.js:742:13)

at /usr/share/myapp.js:10:5

at Layer.handle [as handle_request] (/usr/share/myapp/node_modules/express/lib/router/layer.js:95:5)

at next (/usr/share/myapp/node_modules/express/lib/router/route.js:137:13)

at Route.dispatch (/usr/share/myapp/node_modules/express/lib/router/route.js:112:3)

at Layer.handle [as handle_request] (/usr/share/myapp/node_modules/express/lib/router/layer.js:95:5)

at /usr/share/myapp/node_modules/express/lib/router/index.js:281:22

at Function.process_params (/usr/share/myapp/node_modules/express/lib/router/index.js:335:12)

at next (/usr/share/myapp/node_modules/express/lib/router/index.js:275:10)

at jsonParser (/usr/share/myapp/node_modules/body-parser/lib/types/json.js:109:7)

Could you help me to figure out what the problem is ?

There's a few things going on here. First and foremost, you have declared the app.get and app.post method inside the app.use method, you'll want to separate those, like this:

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

app.post("/", function(request, response) {
    console.log(request.body); 
});

app.get("/index.html",function(req,res) {
    res.end(request.body);
});

You also need to make sure you declare your content type when using res.end in your get method.

Take a look at bodyparser's documentation for an example that looks close to what you are trying to achieve, it may help you: https://github.com/expressjs/body-parser#expressconnect-top-level-generic

You cannot send data received from your external application to a web page by trying to respond to the web page's GET request with request.body of POST request received from the external application. The simplest way to do what you're trying to achieve would be something like this.

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

app.post("/putData", function(request, response) {
    //store data received from the app in the database.
});

app.get("/getData",function(req,res) {
    //retrieve data from the database and send the response to the browser.
});

You can also use sockets to update data on the web page as soon as you receive it in real-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM