Node.js builds web server, interface configuration, parameter format and garbled code processing

Node.js Overview

Node.js is not a new programming language, nor is it a JavaScript library, but a JavaScript running environment, and it is also the second running environment for JavaScript outside of the browser. We can run all the features of ES6 on node.js without worrying about any compatibility issues. In addition to its good support for ES6, node.js also provides some functions that browsers do not have, such as file operations and network operation functions. These functions give node.js the development capabilities of a web server.

How to use node.js

node.js official website: https://nodejs.org/en
As a JavaScript running environment, node.js can execute JS code, but it cannot run HTML and CSS code. HTML and CSS still need to be executed in the browser.

Execute node file name.js in the vscode terminal to execute the file

But if the file content changes, you need to save it and then execute it through the node command again. If you want to automatically execute it in the console after the file is modified, we can use a plug-in of node called nodemon, and you can use node.js Use the package management tool NPM to install it.

Then when executing nodemon filename.js, it will automatically re-execute it for us every time we modify the code.

Build a Web server

To build a web server, you can use the built-in HTTP module of node.js, and then call http.createServer() to create a web server instance.
server.listen sets which port the server instance listens on.

const http = require('http')
const server=http.createServer((req,res)=>{<!-- -->
    res.end('Hello Node!')
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})

Interface request processing (GET)

Determine the request method

const http = require('http')
const server=http.createServer((req,res)=>{<!-- -->

  if (req.method === 'GET') {<!-- -->
    res.end('GET request')
  }else if (req.method === 'POST') {<!-- -->
    res.end('POST request')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})

Solving the problem of garbled characters

const http = require('http')
const server=http.createServer((req,res)=>{<!-- -->

  if (req.method === 'GET') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('GET request')
  }else if (req.method === 'POST') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('POST request')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})

Determine the interface address

pathname

const http = require('http')
const url = require('url')
const server=http.createServer((req,res)=>{<!-- -->
  const {<!-- -->pathname}=url.parse(req.url)
  if (req.method === 'GET' & amp; & amp; pathname === '/getInfo') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('Responded to the request of getInfo interface')
  }else if (req.method === 'POST') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('POST request')
  }else{<!-- -->
    res.statusCode = 404
    res.end('Not Found')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})


Parameters

const http = require('http')
const url = require('url')
const server=http.createServer((req,res)=>{<!-- -->
  const {<!-- -->pathname,query}=url.parse(req.url)
  if (req.method === 'GET' & amp; & amp; pathname === '/getInfo') {<!-- -->
    console.log(query);
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('Responded to the request of getInfo interface')
  }else if (req.method === 'POST') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('POST request')
  }else{<!-- -->
    res.statusCode = 404
    res.end('Not Found')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})

Request parameter format conversion

To convert the urlencoded format into a JavaScript object, you can use the node.js built-in module querystring.

const http = require('http')
const url = require('url')
const qs = require('querystring')
const server=http.createServer((req,res)=>{<!-- -->
  const {<!-- -->pathname,query}=url.parse(req.url)
  if (req.method === 'GET' & amp; & amp; pathname === '/getInfo') {<!-- -->
    console.log(query);
    console.log(qs.parse(query));
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('Responded to the request of getInfo interface')
  }else if (req.method === 'POST') {<!-- -->
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('POST request')
  }else{<!-- -->
    res.statusCode = 404
    res.end('Not Found')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})

Post request processing

Post is different from get. The get request parameter is a splicing path, while the request parameter of post is placed in the request body. The types of transferable request parameters are richer and the volume is larger.

const http = require('http')
const url = require('url')
const qs = require('querystring')
const server=http.createServer((req,res)=>{<!-- -->
  const {<!-- -->pathname,query}=url.parse(req.url)
  if (req.method === 'GET' & amp; & amp; pathname === '/getInfo') {<!-- -->
    console.log(query);
    console.log(qs.parse(query).a);
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('Responded to the request of getInfo interface')
  }else if (req.method === 'POST' & amp; & amp; pathname === '/postInfo') {<!-- -->
    let data=''
    req.on('data',temp=>{<!-- -->
      data + = temp
    })

    req.on('end',()=>{<!-- -->
      console.log( qs.parse(data));
      
    res.setHeader('Content-Type','text/plain;charset=utf-8')
    res.end('POST request')
    })

  }else{<!-- -->
    res.statusCode = 404
    res.end('Not Found')
  }
    
})

server.listen(3000,()=>{<!-- -->
  console.log('Server is running');
})