Example of http form submission in Nodejs
This article mainly introduces the http form submission of Nodejs in detail, which has certain reference value. Interested friends can refer to
The request of the http module was introduced before The process of responding to also introduces the data transmission between the client and server of the TCP protocol. The http protocol is the upper layer protocol of TCP. Here we create a simple web server and process the submitted form data, as summarized in the great Node.js book.
POST method to submit form data
As I have summarized before, you need to use the POST method to submit data to the server, and the request information for the GET methodAll in the queryString, there is no request body, and the data transmitted by the POST method is all in the request body, so the POST method needs to be used when submitting form data.
req is the request information, req.url represents the requested address. After the server is running, the URL requested by req is 127.0.0.1:3000. At this time, req.url is '/', then return is a string of form data. The method set in the form data is post, action is '/url', the surface submission method is POST, and the address to submit the data is 127.0.0.1:3000/ url, and after submission, a new page needs to be obtained, which is 127.0.0.1:3000/url. At this time, req.url is '/url', so another page is displayed.
//提交表单数据 var http=require('http'); var querystring=require('querystring'); var server=http.createServer(function (req,res) { //req.url不同则返回的页面不同 if('/'==req.url){ res.writeHead(200,{'Content-Type':'text/html'}); res.write([ '<form method="post" action="/url">', '<h1>My Form</h1>', '<fieldset>', '<label>Personal Information</label>', '<p>What is your name?</p>', '<input type="text" name="name">', '<button>submit</button>', '</form>' ].join('')); res.end(); }else if('/url'==req.url&&req.method=='POST'){ var reqBody=''; req.on('data',function (data) { reqBody += data; }); req.on('end',function () {//用于数据接收完成后再获取 res.writeHead(200,{'Content-Type':'text/html'}); res.write('you have sent a '+req.method+' request\n'); res.write('<p>Content-Type:'+req.headers['content-type']+'</p>' +'<p>Data:your name is '+querystring.parse(reqBody).name+'</p>'); res.end(); }) }else{ res.writeHead(404); res.write('Not Found'); res.end(); } }).listen(3000,function () { console.log('server is listening 3000'); });
After submission, you need to obtain the request body of the request information, because the information in the POST method is in the request body. Use req to bind the data event to obtain the data. You need to pay attention here. What is important is that the data must be operated after the data reception is completed, that is, the end event must be bound to monitor whether the request information has been transmitted.
Querystring is a query string module, used to parse query strings. The parse method parses the query string into an object. Run the server in git and get the page like this:
After submitting the data:
The above is the detailed content of Example of http form submission in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
