Using node.js developers blog

ECMAScript syntax specification

nodejs = ECMAScript + nodejsAPI, any operation can be done in the server

js = ECMAScript + WebAPI

common.js is nodejs modular specification

Instructions

a.js written

function add(a, b) {
  return a + b;
}

module.exports = add;

b.js written 

var add = require("./a");
var sum = add(10, 20);
console.log(sum);

In the terminal 30 performs output node b.js

Export multiple modules

a.js written

function add(a, b) {
  return a + b;
}
function mul(a, b) {
  return a * b;
}

module.exports = { add, mul };

b.js export module

var { add, mul } = require("./a");

The above code corresponds to

var ops = require("./a");
var add = ops.add;
var mul = ops.mul;

Notice that the Export module name must match the name of the module import

Importing other plug-ins

Npm environment initialization

npm init -y

Installation lodash

npm i lodash

Introducing the same manner as

var _ = require("lodash");

Development interface (without any framework)

How to handle http request is nodejs

http Request Overview

1. The client DNS resolution to establish a TCP connection, send http request

2.server receives http request, processing, and returns

3. the client received the returned data, process the data (such as to render the page, execute js) 

Note:

TCP connection will visit our web site, such as Baidu, is a domain name, the domain name to find the IP address of a corresponding ip address, the client through the DNS resolution: DNS resolve

nodejs handles the routing

var http = require("http");
var queryString = require("querystring");

var server = http.createServer((req, res) => {
  var url = req.url;
  var path = url.split("?")[0];
  req.query = queryString.parse(url.split("?")[1]);
});

nodejs process get requests

var http = require("http");
var queryString = require("querystring");

var server = http.createServer((req, res) => {
  var method = req.method;
  console.log(method);
  var url = req.url;
  req.query = queryString.parse(url.split("?")[1]);
  res.end(JSON.stringify(req.query));
});

server.listen(8080, function() {
  console.log("listening on 8080");
});

post processing request nodejs 

Use postman plug, Baidu search postman chrome crx direct download or download postman app

var http = require("http");
var queryString = require("querystring");

var server = http.createServer((req, res) => {
  if (req.method === "POST") {
    console.log("content-type", req.headers["content-type"]);
  }
  var postData = "";
  res.on("data", function(chunk) {
    postData += chunk.toString();
  });
  res.on("end", function() {
    console.log(postData);
    res.end(JSON.stringify(postData));
  });
});

server.listen(8080, function() {
  console.log("listening on 8080");
});

nodejs process returns a response format

// 设置返回的格式为json
res.setHeader('Content-type','application/json')

Setup the development environment

Use nodeMon monitoring file changes, automatically restart node

Use cross-env set environment variables, compatible with mac linux and windows

Development interface (temporarily connect to the database, will not consider login)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 121 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/Vansal/article/details/102157487