1. Node environment construction
Install the NVM in Windows
Juejin. Cn/post / 688821…
The difference between Node and javascript
ECMAScript
- 1. The syntax is defined and must be followed when writing javascript and NodeJS
- 2. Variable definition, loop, judgment, function
- 3. Stereotypes and prototype chains, scopes and closures, asynchrony
- 4. You cannot manipulate the DOM, listen for click events, or send Ajax requests
- 5. Cannot process HTTP requests or operate files
- 6. That is, ECMAScript alone makes almost no actual projects possible
javascript
- 1. Use the ECMAScript syntax specification, plus Web API
- 2.DOM operation, BOM operation, event binding, Ajax, etc
- 3. Combine the two to complete any operation on the browser
nodejs
- 1. Use the ECMAScript syntax specification, plus the NodeJS API
- 2. Processing HTTP, processing files, etc
- 3. You can perform any operation on the server by combining the two
conclusion
- 1.ECMAScript is a syntax specification
- 2.nodejs = ECMAScript + nodejs API
Third, commonjs
Note: The CommonJS modular specification is the specification used in NodeJS
Create a project
- 1. Initialize the project
npm init -y
- 2. Install test cases
lodash
npm i -S lodash
-
3. Create service files serve.js and tool files utils.js
-
4. Compile corresponding tool files and introduce tool methods into services
Project code
- 1.
utils.js
code
Two public methods add Minus were created
function add(a, b) { return a + b; } function minus(a, b) { return a - b; } // commonjs exports module.exports = {add, minus}Copy the code
- 2.
serve.js
Introduction and use of
// commonjs introduces const {add, minus} = require('./utils') const _ = require('lodash') const resultAdd = add(2, 1) const resultMinus = minus(2, 1) console.log(resultAdd, resultMinus) const arr = _.concat([1, 2], 3) console.log('arr', arr)Copy the code
- 3. Run the code to show the effect
node serve.js
- 4. Project code directory
HTTP module in nodeJS
Supplementary interview question – Overview of Browser HTTP requests?
- 1.DNS resolution, TCP connection establishment, and HTTP request sending
- 2. The server receives the HTTP request, processes it, and returns it
- 3. The client receives returned data, processes the data, and renders the data
Get in the HTTP request
- 1. Get requests and QueryStrings
A get request is a request for the client to fetch data from the server, such as the home page list, and pass the data to the QueryString, such as index.html? Content =”javascript”&category=”1
- 2. Code cases
const http = require('http') const querystring = require('querystring') const server = http.createServer((req, Res) => {console.log(req.method) const url = req.url req.query = queryString.parse (url.split('? ')[1]) res.end(JSON.stringify(req.query)) }) server.listen(3000, () => { console.log('server start') })Copy the code
- 3. Effect display
Post in an HTTP request
- Post request and PostData
Post request, that is, the client to transfer data to the server, such as a new news record through post data transfer data browser cannot directly simulate, handwritten JS, or use Postman
- 2. Code cases
const http = require('http') const server = http.createServer((req, Res) => {if(req.method === 'POST') {// Data format console.log('content-type', Req.headers ['content-type']) // let postData = "" req.on('data', chunk => { postData += chunk.toString() }) req.on('end', () => { console.log(postData) res.end('hello world') }) } }) server.listen(3000, () => { console.log('server start'); })Copy the code
- 3. Effect display
Nodejs handles routing
www.learnyi.com/ www.learnyi.com/username www.learnyi.com/username/xx…
Description: Represents the unique identifier of the URL
A comprehensive example of HTTP
- 1. Code cases
const http = require('http') const querystring = require('querystring') const server = http.createServer((req, res) => { const method = req.method const url = req.url const path = url.split('? ')[0] const query = querystring.parse(url.split('? ')[1]) // Set the return format to JSON res.setheader (' content-type ', 'application ') // Const resData = {method, URL, path, query } if (method === 'GET') { res.end( JSON.stringify(resData) ) } if (method === 'POST') { let postData = '' req.on('data', chunk => { postData += chunk.toString() }) req.on('end', () => { resData.postData = postData res.end( JSON.stringify(resData) ) }) } }) server.listen(3000, () => { console.log('server start'); })Copy the code
- 2. Effect display