dynamically replaced the function and principle of HTML content

1. implementation and implemented the data of the user form, combined with the user, to display the user input data to the corresponding location. .

principle: replacing template data in HTML, such as the username {name}, can replace the original template data by

replace (regular expression, value) and output to the client by replacing the template data in HTML, such as the username {name}.

2. mainly used methods and modules

2.1 file operation module var fs=require ("Fs"); Number Description: the path of the

path: file (including the name of the file) the

callback: file reads the data read from the

file of the finished callback function. From this method, it will automatically fill the

 callback:function (err, data) {/ / / / / / / / / / / / / / / / / / / / / to the data. Such as printing console.log (data);}}

2.2 data processing module var querystring=require ("querystring");

2.3 post requests the main method,

, to monitor the method of uploading post data: request.on ('data', funcation (chunk) {//chunk post data fragment}); Core code requestHandler.js

 / * request processing * / / / / / loading node non blocking thread module //var exec=require ("child_process").Exec; / / / / loading queryString module and file upload module var HTTP = require ("HTTP"); R path = require ("path"); VAR querystring=require ("querystring"); function login (request, response) {var}; / / / / / / / / * replacement data template Field * / var arr=["name", "password"]; function recall (data) {dataStr = data.toString (); / / / / traversal of the template field for (VaR I = 0). TML "}); response.write (dataStr); response.end ();} fs.readFile (" login.html ", function (err, data) {recall (data)});}. }); response.write (data); response.end ();})} exports.login = login; exports.index=index; 

4.html templates. T; < body> the username: {name}< br /> < {name}. {password} is the template field to replace --> T; < input type= "submit" value= "submit" /> < /form> < /body> <

you might be interested in:


This concludes the body part

This paper fixed link:http://www.script-home.com/a-simple-method-to-implement-dynamic-html-in-nodejs.html | Script Home | +Copy Link

Article reprint please specify:A simple method to implement dynamic HTML in nodejs | Script Home

You may also be interested in these articles!