Node.js server that accepts POST requests

后端 未结 2 1284
梦谈多话
梦谈多话 2020-11-28 20:30

I\'m trying to allow javascript to communicate with a Node.js server.

POST request (web browser)

var http = new XMLHttpRequest();
v         


        
相关标签:
2条回答
  • The following code shows how to read values from an HTML form. As @pimvdb said you need to use the request.on('data'...) to capture the contents of the body.

    const http = require('http')
    
    const server = http.createServer(function(request, response) {
      console.dir(request.param)
    
      if (request.method == 'POST') {
        console.log('POST')
        var body = ''
        request.on('data', function(data) {
          body += data
          console.log('Partial body: ' + body)
        })
        request.on('end', function() {
          console.log('Body: ' + body)
          response.writeHead(200, {'Content-Type': 'text/html'})
          response.end('post received')
        })
      } else {
        console.log('GET')
        var html = `
                <html>
                    <body>
                        <form method="post" action="http://localhost:3000">Name: 
                            <input type="text" name="name" />
                            <input type="submit" value="Submit" />
                        </form>
                    </body>
                </html>`
        response.writeHead(200, {'Content-Type': 'text/html'})
        response.end(html)
      }
    })
    
    const port = 3000
    const host = '127.0.0.1'
    server.listen(port, host)
    console.log(`Listening at http://${host}:${port}`)
    
    
    

    If you use something like Express.js and Bodyparser then it would look like this since Express will handle the request.body concatenation

    var express = require('express')
    var fs = require('fs')
    var app = express()
    
    app.use(express.bodyParser())
    
    app.get('/', function(request, response) {
      console.log('GET /')
      var html = `
        <html>
            <body>
                <form method="post" action="http://localhost:3000">Name: 
                    <input type="text" name="name" />
                    <input type="submit" value="Submit" />
                </form>
            </body>
        </html>`
      response.writeHead(200, {'Content-Type': 'text/html'})
      response.end(html)
    })
    
    app.post('/', function(request, response) {
      console.log('POST /')
      console.dir(request.body)
      response.writeHead(200, {'Content-Type': 'text/html'})
      response.end('thanks')
    })
    
    port = 3000
    app.listen(port)
    console.log(`Listening at http://localhost:${port}`)
    
    
    0 讨论(0)
  • 2020-11-28 20:51

    Receive POST and GET request in nodejs :

    1).Server

        var http = require('http');
        var server = http.createServer ( function(request,response){
    
        response.writeHead(200,{"Content-Type":"text\plain"});
        if(request.method == "GET")
            {
                response.end("received GET request.")
            }
        else if(request.method == "POST")
            {
                response.end("received POST request.");
            }
        else
            {
                response.end("Undefined request .");
            }
    });
    
    server.listen(8000);
    console.log("Server running on port 8000");
    

    2). Client :

    var http = require('http');
    
    var option = {
        hostname : "localhost" ,
        port : 8000 ,
        method : "POST",
        path : "/"
    } 
    
        var request = http.request(option , function(resp){
           resp.on("data",function(chunck){
               console.log(chunck.toString());
           }) 
        })
        request.end();
    
    0 讨论(0)
提交回复
热议问题