Node implements simple front-end and back-end interaction

小云云
Release: 2017-12-21 11:13:56
Original
1446 people have browsed it

Node is a skill that must be learned in the front-end. We all know that node uses js as the back-end. Before learning node, we need to understand how node realizes front-end and back-end interaction. This article brings you a simple front-end and back-end interaction of Node (explanation with examples). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Here is a simple interaction between native ajax and node. Friends who have just learned node can take a look. On the one hand, you understand how the server and client interact, and on the other hand, you are more familiar with node development.

Post the code first: (If you are interested, you can copy it locally and run it yourself)

The html of the main page

index.html:

     

Copy after login

Next is the server-side code. The running method is to enter the command in the node environment: node server.js

server.js:

let http = require('http'); let qs = require('querystring'); let server = http.createServer(function(req, res) { let body = ''; // 一定要初始化为"" 不然是undefined req.on('data', function(data) { body += data; // 所接受的Json数据 }); req.on('end', function() { res.writeHead(200, { // 响应状态 "Content-Type": "text/plain", // 响应数据类型 'Access-Control-Allow-Origin': '*' // 允许任何一个域名访问 }); if(qs.parse(body).name == 'food') { res.write('apple'); } else { res.write('other'); } res.end(); }); }); server.listen(3000);
Copy after login

The qs module introduced is used to parse JSON

req.on('data', callback); // Monitor the client's data and execute the callback function once data is sent

req.on('end', callback); // Data reception completed

res //Response

Client js (the function is responsible for some DOM operations and sending ajax requests)

client.js:

let btn1 = document.getElementById('btn1'); let btn2 = document.getElementById('btn2'); let content = document.getElementById('content'); btn1.addEventListener('click', function() { ajax('POST', "http://127.0.0.1:3000/", 'name='+this.innerHTML); }); btn2.addEventListener('click', function() { ajax('POST', "http://127.0.0.1:3000/", 'name='+this.innerHTML); }); // 封装的ajax方法 function ajax(method, url, val) { // 方法,路径,传送数据 let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) { content.innerHTML = xhr.responseText; } else { alert('Request was unsuccessful: ' + xhr.status); } } }; xhr.open(method, url, true); if(val) xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(val); }
Copy after login

This simple interaction is like this. In fact, when we first learn a back-end language, the first thing we do is to write a front-end and back-end interactive program. This will help us better understand the division of labor between the front and back ends.

run method:

First run server.js, and then open html to request a response.

Related recommendations:

Related content summary about front-end and back-end interaction

##Node.js+Koa framework realizes front-end and back-end interaction

PHP front-end and back-end interaction

The above is the detailed content of Node implements simple front-end and back-end interaction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!