Node. js Express framework POST method details, node. jsexpress
POST method
The following example demonstrates submitting two parameters in the form using the POST method. We can use the process_post router in the server. js file to process the input:
Modify the code of the index.htm file as follows:
Modify the server. js File Code as follows:
Var express = require ('express '); var app = express (); var bodyParser = require ('body-parser '); // create application/x-www-form-urlencoded encoding parsing var urlencodedParser = bodyParser. urlencoded ({extended: false}) app. use (express. static ('public'); app. get ('/index.htm', function (req, res) {res. sendFile (_ dirname + "/" + "index.htm");}) app. post ('/process_post', urlencodedParser, function (req, res) {// output JSON format response = {first_name: req. body. first_name, last_name: req. body. last_name}; console. log (response); res. end (JSON. stringify (response);}) var server = app. listen (8081, function () {var host = server. address (). address var port = server. address (). port console. log ("application instance, access address: http: // % s: % s", host, port )})
Run the preceding code:
$ node server.js
Application Instance, access address: http: // 0.0.0.0: 8081
Browser access http: // 127.0.0.1: 8081/index.htm
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.