This is the 7th day of my participation in Gwen Challenge

Create a new folder NPM init-y

Creating an HTTP Service

const http= require('http')
const server = http.createServer((req,res) = > {
  // Set the response header
  res.setHeader('Content-Type'.'application/json')
  // Return data
  res.end('

Returns content

'
)})// Listen on the port server.listen(1996) console.log('OK'); Copy the code

Processing GET requests

const http= require('http')
const querystring = require('querystring')
const server = http.createServer((req,res) = > {
  res.setHeader('Content-Type'.'application/json')
  console.log('method', req.method)
  const url = req.url
  console.log('url', url);
  Parse queryString.parse converts a=1&b=2&c=3 to {a:1,b:2,c:3}
  req.query = querystring.parse(url.split('? ') [1])
  console.log('query', req.query);
  res.end(JSON.stringify(req.query))
})
server.listen(1996)
console.log('OK');
Copy the code

Handling POST requests

const http = require('http')
const server = http.createServer((req,res) = > {
  // Use req.method to determine the request type
  if(req.method == 'POST') {
    console.log('req content-type: ', req.headers['content-type']);
    // Accept data
    let postData = ' '
    // Listen for req data and end events. When data time transfer is complete, reQ end events will be triggered
    req.on('data'.chunk= > {
      postData += chunk.toString()
    })
    req.on('end'.() = > {
      console.log('postData: ', postData);
      res.end('Hello World! ')
    })
  }
})
server.listen(1996)
console.log('OK');
Copy the code

A comprehensive

const http = require('http')
const querystring = require('querystring')
const server = http.createServer((req, res) = > {
  const method = req.method
  const url = req.url
  const path = req.url.split('? ') [0]
  const query = querystring.parse(url.split('? ') [1])
  // Set the response header
  res.setHeader('Content-Type'.'application/json')
  // The data returned
  const resData = {
    method,
    url,
    path,
    query
  }
  // Handle GET and POST separately
  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(1996)
console.log('OK')
Copy the code

Officially open to

Setting up the development environment

  • Initialize the NPM environment NPM init-y

  • Add the bin folder in your home directory and create www.js, then modify package.json to change the entry file address

    {
      "name": "node-blog"."version": "1.0.0"."description": ""."main": "bin/www.js"."scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": []."author": ""."license": "ISC"
    }
    Copy the code
  • www.js and app. Js

    // www.js
    const http = require('http')
    
    const PORT = 1996
    
    const serverHandle = require('.. /app.js')
    
    const server = http.createServer(serverHandle)
    
    server.listen(PORT)
    Copy the code

    Create app.js, the same as the bin directory

    // app.js
    const serverHandle = (req, res) = > {
      res.setHeader('Content-Type'.'application/json')
      const resData = {
        name: 'Tmier'.site: 'xxx.com'
      }
      res.end(JSON.stringify(resData))
    }
    module.exports = serverHandle
    Copy the code
  • Installing a plug-in

    npm i nodemon cross-env --save-dev
    Copy the code

    After a successful installation, add a new command to package.json

      "scripts": {...// Set the environment variable to dev and start www.js using nodemon
        "dev": "cross-env NODE_ENV=dev nodemon ./bin/www.js"
      }
    Copy the code

    It can then be run using:

    npm run dev
    // Nodemon can listen for changes in js files and update them automatically
    Copy the code

    The development environment is set up and the routing will start tomorrow

    Because it is really just beginning to learn Node, so notes slightly immature, step by step, come on ~