简体   繁体   中英

How shall I understand and use this server written in Node.js?

I am trying to understand a simple server written in node.js

var http = require('http'); 
var querystring = require('querystring'); 

http.createServer(function (req, res) { 
  switch(req.url) { 
    case '/form': 
        if (req.method == 'POST') { 
         console.log("[200] " + req.method + " to " + req.url); 
         var fullBody = ''; 
         req.on('data', function(chunk) { 
           fullBody += chunk.toString(); 
         }); 
         req.on('end', function() { 
           res.writeHead(200, "OK", {'Content-Type': 'text/html'});   
           res.write('<html><head><title>Post data</title></head><body>'); 
           res.write('<style>th, td {text-align:left; padding:5px; color:black}\n'); 
           res.write('th {background-color:grey; color:white; min-width:10em}\n'); 
           res.write('td {background-color:lightgrey}\n'); 
           res.write('caption {font-weight:bold}</style>'); 
           res.write('<table border="1"><caption>Form Data</caption>'); 
           res.write('<tr><th>Name</th><th>Value</th>'); 
           var dBody = querystring.parse(fullBody); 
           for (var prop in dBody) { 
            res.write("<tr><td>" + prop + "</td><td>" + dBody[prop] + "</td></tr>"); 
           } 
           res.write('</table></body></html>'); 
           res.end(); 
         }); 
       } else { 
         console.log("[405] " + req.method + " to " + req.url); 
         res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); 
         res.end('<html><head><title>405 - Method not supported</title></head><body>' + 
                 '<h1>Method not supported.</h1></body></html>'); 
       } 
      break; 
    default: 
      res.writeHead(404, "Not found", {'Content-Type': 'text/html'}); 
      res.end('<html><head><title>404 - Not found</title></head><body>' + 
              '<h1>Not found.</h1></body></html>'); 
      console.log("[404] " + req.method + " to " + req.url); 
  }; 
}).listen(8080); 

What does the function req.on() do? For example, req.on('data',...) and req.on('end',...) ? Is this explained somewhere in https://nodejs.org/api/http.html ? I think I might have missed it.

How shall I send a HTTP request to the server so that the part inside case '/form': if (req.method == 'POST'){...} is executed? Specifically, when using curl , what arguments and options shall I give to curl ? What if using Firefox browser?

Req.on('data') means your server is receiving the data from the client, in callback attached to req.on('data') you generally concatenate the data and then parse the data to be used later. Once it has received the entire data then the callback attached to req.on('end') will be executed and here you can do all the business logic based on the data you received and then send the response back to the client

now how can you access the /form URL?

When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:

  1. application/json
  2. application/x-www-form-urlencoded

you can either use postman client or curl to access the /form URL.

in curl-

curl -H "Content-Type:application/x-www-form-urlencoded" \
-d "param1=value1&param2=value2" \
http://localhost:8080/form

curl -H "Content-Type:application/json" \
-d '{"key1":"value1", "key2":"value2"}' \
http://localhost:8080/form

In your code you are accepting the application/x-www-form-urlencoded so go with the first one

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