expressrelatedapiintroduce

express.json

This is an Express middleware feature that processes THE JSON data in the request and mounts it in the body.

import * as express from "express";

const app = express();

app.use(express.json()); // Parse the JSON data in the request
app.use((request, response, next) = > {
  response.send(request.body);
});
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

If you do not use middleware, you need to process the JSON data in the request by listening for the data and end events of the request to get the complete request data.

import * as express from "express";

const app = express();

app.use((request, response, next) = > {
  const data = [];
  request.on("data".(chunk) = > {
    data.push(chunk);
  });
  request.on("end".() = > {
    response.send(JSON.parse(data[0].toString()));
  });
});
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

express.urlencoded

Handles requests in X-www-form-urlencoded form

Data in this format is encoded to separate key-value pairs with “&” and keys and values with “=”, for example, name=zhangsan&age=18

express.static

Set a static directory. When accessing a resource, the user will go to this static directory to find the corresponding resource

import * as express from "express";

const app = express();

app.use(express.static("public")); // Set the static resource directory
app.use((request, response, next) = > {
  response.send(request.body);
});
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

Static Resource directory

public
  --home.html
  --about.html
Copy the code

When the browser accesses /home/html, it automatically matches the public /home/html file and returns it to the front end.

apprelatedapiintroduce

app.locals

Set local variables that are accessible throughout the application.

app.locals.name = "Zhang";

console.log(app.locals.name);
/** is equivalent to */
console.log(request.app.locals.name);
Copy the code

app.set/app.get

  1. Set any value you want to store, similar to app.locals, which is accessible throughout the application.

    app.set("name"."zhangsan");
    app.get("name");
    Copy the code
  2. Special access value

  • Views: All views are stored in the public directory, and the view engine is PUG

    app.set("views"."public");
    app.set("view engine"."pug");
    Copy the code
    • env: Sets environment variables
    • etagSet:etag

app.param

Is used to process the arguments in the path, and if they match, the subsequent callback function is executed.

import * as express from "express";

const app = express();
app.param("id".(req, res, next, id) = > {
  console.log(id);
  next();
});
app.get("/user/:id".(req, res, next) = > {
  res.send("1");
});

app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

app.route/app.all/app.get/app.post

app
  .route("/user")
  .all((req, res, next) = >{... })// Handle all situations
  .get((req, res, next) = >{... })// Process get requests
  .post((req, res, next) = >{... });// Process the POST request
Copy the code

requestrelatedapi

request.baseUrl

Set the public API access path

import * as express from "express";

const app = express();
const router = express.Router();
router.get("/jp".(req, res) = > {
  console.log(req.baseUrl); // /greet
  res.send(req.baseUrl);
});

app.use("/greet", router);
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

request.ip

Get the request IP, but this IP is not allowed, only know a general range, for example in a school, you may get the same IP.

request.params

Gets the parameters in the request path

import * as express from "express";

const app = express();
app.use("/greet/jp/:id".(req, res, next) = > {
  console.log(req.params);
  res.send(req.params);
});
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

request.path

Used to get the request path. Cannot be used in app.use.

import * as express from "express";

const app = express();
app.get("/greet".(req, res, next) = > {
  console.log(req.path);
  res.send(req.path);
});
app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

request.query

Used to obtain query parameters.

request.xhr

Determines whether the request header x-requested-with value is XMLHttpRequest.

request.acceptsLanguages()

/ / request headerAccept-Language: en-GB,en-US; q=0.9,en; q=0.8,zh-CN; q=0.7,zh; q=0.6

// The backend can determine which languages are currently supported
request.acceptsLanguages()  // ['en-GB', 'en-US', 'en', 'zh-CN', 'zh']
request.acceptsLanguages("zh")  // zh
request.acceptsLanguages("fr")  // false
Copy the code

responserelatedapi

response.format

Accept returns the corresponding content according to the request header

import * as express from "express";

const app = express();
app.get("/test".(req, res, next) = > {
  res.format({
    "text/plain": function () {
      res.send("hey");
    },
    "text/html": function () {
      res.send("<p>hey</p>");
    },
    "application/json": function () {
      res.send({ message: "hey" });
    },
    default: function () {
      res.status(406).send("Not Acceptable"); }}); }); app.listen(3000.() = > {
  console.log("Listening successful.");
});
Copy the code

response.local

Used with status code 301 for redirection, equivalent to res.redirect