Node implements simple front-end and back-end interaction
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:
<meta> <title></title> <button>food</button> <button>other</button> <h1></h1> <script></script>
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);
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 <p>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. </p><p>run method: </p><p>First run server.js, and then open html to request a response. </p><p>Related recommendations: </p><p><a href="http://www.php.cn/js-tutorial-366080.html" target="_self">Related content summary about front-end and back-end interaction</a></p><p>##Node.js+Koa framework realizes front-end and back-end interaction<a href="http://www.php.cn/js-tutorial-354077.html" target="_self"></a></p><p>PHP front-end and back-end interaction<a href="http://www.php.cn/php-weizijiaocheng-326402.html" target="_self"></a></p>
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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
