简体   繁体   中英

How to properly send and receive "POST" requests to and from a server

I am trying to make a server for a website. The server works perfectly, and it can receive and send data normally. I have hosted the server, and I made a demo client-side repl on Replit to test the flow. But somehow, either the reply isn't receiving or sending properly, or the server isn't working. (For the client-side code, I am using JQuery)

// Client Side Code
const url = "My url";
const data = {
    "file": "lol.html"
}

function start() {
    console.log("test 1")
    $.post(url, data, function(data, status) {
        console.log(data + "is data & status is " + status);
    });
}
// Server-side code
var http = require('http'); // Import Node.js core module
var fs = require('fs'); // File System
var url = require('url');

var server = http.createServer(function(req, res) {   //create web server
    if (req.url == '/') { //check the URL of the current request

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.write(JSON.stringify({ message: "This is the Backend server for my website" }));
        res.end();

    }
    else if (req.url == "/ping") {

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.write(JSON.stringify({ message: "Pong!" }));
        res.end();

    }
    else if (req.url.startsWith("/read")) {

        var q = url.parse(req.url, true);
        var qdata = q.query;
        fs.readFile(String(qdata.file), function(err, data) {
            if (err) {
                res.writeHead(404, { 'Content-Type': 'text/html' });
                return res.end("404 Not Found");
            }
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.write(data);
            return res.end();
        });
    }
    else if (req.url.startsWith("/get")) {

        var q = url.parse(req.url, true);
        var qdata = q.query;
        fs.readFile(String(qdata.file), function(err, data) {
            if (err) {
                res.writeHead(404, { 'Content-Type': 'text/html' });
                return res.end("404 Not Found");
            }
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.write(data);
            return res.end();
        });
    }
    else {
        res.end('Invalid Request!');
    }

});

server.listen(5000); //6 - listen for any incoming requests

console.log('Node.js web server at port 5000 is running..')

Can Someone tell me how to perform this properly?

Use axios instead of old data fetching technologies. It's a promise based HTTP client for the browser and node.js.

Performing a POST request

axios.post('/YOUR_FULL_API_URL', {
  data1: 'value1',
  data2: 'value2'
})
.then(function (response) {
   console.log(response);
})
.catch(function (error) {
   console.log(error);
});

Full documentation: https://axios-http.com/
NPM package readme: https://www.npmjs.com/package/axios

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