Node Note-Pray Lin
Naming rules: File or function or variable names must start with a letter only
Module:
Generally a JS file is a module, and a number of JS files can require each other (that is, mutual reference), they jointly achieve a large function
In Node.js, variables or functions defined in a JavaScript file are valid only in the current file. Each JS file is a closure structure, so when we need to reference these variables or functions from the js file, we must use the exports object to expose the object. The caller needs to import the JavaScript file by naming it require()
A file –require–> B file –require–> C file
Exports in Node.js differ from module.exports
Every Node.js execution file automatically creates a module object and the module object creates a property named exports which starts with {} exports which is module.exports
Module. Exports can export functions, object instances, basic types of variables, etc. Module. Exports can be exported from module
If you want to use exports as a module return, set an attribute for it and do not export properties or function instances with the same name on module.exports.
About the require import path issue
“Node_mudules” will be used to query the file path imported by require./ “node_mudules” will be searched upward if node_mudules is not available
//npm install silly-datetime const sd = require("silly-datetime"); Year: year month: month day: day hour: hour Minute Y: year M: month D: day H: hour M: Let now= sd.format(new Date(),"YYYYMMDDHHmm"); Console. log(" Beijing time: "+ now);Copy the code
The nodeJS form submits a POST request
HTML :
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, <meta http-equiv=" x-UA-compatible "content="ie=edge"> <title> Form submit </title> <link rel=" heet" Href =" CSS /master.css"/> </head> <body> <div class="wrapper"> <div class="title"> registry </div> <form class="form" Action = "http://localhost:3000/dopost" method = "post" > < ul > < li > < input type = "text" name = "name" placeholder = "please enter your name" Class =" TXT "/></li> <li><input type="password" name="password" placeholder=" please input your password type="submit" placeholder="" class="btn"/></li> </ul> <div class="cl"></div> </form> </div> </body> </html>Copy the code
JS:
const http = require("http"); const queryString = require("querystring"); // The module provides a utility for parsing and formatting URL query strings const server = http.createserver ((req, res) => {console.log(req.method); //req.method: How to get the submission: Get /post if (req.url == "/dopost" &&req.method.tolowerCase () == "post") {/* */ / let allData = ""; AddListener ("data", chunk => {allData += chunk; /* We may see only one segment on the local computer, but N */ req.addListener("data", chunk => {allData += chunk; }); Req.addlistener ("end", () => {let dataString = allData.toString(); console.log("allData loading...." ); console.log(allData); // Convert dataString to an object let dataObj = queryString.parse(dataString); console.log("dataObj loading....." ); console.log(typeof dataObj); Console. log(" name: "+ dataobj.name); Console. log(" password: "+ dataobj.password); res.end("success"); }); }}); Server. Listen (3000, "127.0.0.1");Copy the code