当前位置:网站首页>Express get request
Express get request
2022-06-30 09:15:00 【The man of Jike will never admit defeat】
For yourself Express Of Get request
Last article yes Express Of Hello World. Here we'll see how to Express In dealing with Get request .
General operation
General operation , I haven't mentioned it since last time request Start with the object ( The following content has deleted about cookie The department in charge of the , I hope I have the opportunity to study cookie Well ).
app.get('/index', function (req, res) {
console.log(JSON.stringify(req.app === app, null, 2)); // true
console.log(`req.baseUrl: ${
req.baseUrl}`); // empty , Related to routing
console.log(`req.hostname: ${
req.hostname}`);
// 192.168.1.5 | localhost
console.log(`req.ip: ${
req.ip}`);
// ::ffff:192.168.1.5 | ::1
console.log(`req.ips: ${
req.ips}`); // empty
console.log(`req.method: ${
req.method}`); // GET
console.log(`req.originalUrl: ${
req.originalUrl}`); // /index
console.log(`req.params: ${
JSON.stringify(req.params || [], null, 2)}`);
console.log(`req.path: ${
req.path}`); // /index
console.log(`req.protocol: ${
req.protocol}`); // http
console.log(`req.query: ${
JSON.stringify(req.query || [], null, 2)}`);
// { "username": "tom", "password": "jerry" }
console.log(`req.route: ${
JSON.stringify(req.route || [], null, 2)}`);
console.log(`req.secure: ${
req.secure}`); // false
console.log(`req.subdomains: ${
req.subdomains}`);
console.log(`req.xhr: ${
req.xhr}`); // false
res.end();
});
First of all, I would like to introduce about ip and hostname Content , Because I was a little confused when I saw it for the first time
| monitor host | ip | hostname |
|---|---|---|
| Default | ::1 | localhost |
| 127.0.0.1 | 127.0.0.1 | localhost |
| 192.168.1.5( Connect WiFi Time machine IP) | ::ffff:192.168.1.5 | 192.168.1.5 |
Refer to the Information and Node Official website Description of , Just know the following content .IPv6 It's made up of two parts : Subnet prefix and Interface suffix .IPv6 altogether 128bit, These two parts are 64bit. If IPv4 The address is embedded in IPv6 in , To make any IPv6 The parser knows that it is processing a IPv4 The address of , So add... To the front Subnet prefix . Subnet prefix Continuous in 0 It can be omitted , therefore ::ffff: It's actually 0000:0000:ffff:0000.
that ::1 Empathy , yes IPv6 Of localhost An abbreviated version of , The full version should be 0:0:0:0:0:0:0:1.
obtain Get Request parameters
Use request.query obtain URL Parameters
app.get('/getUser', (req, res) => {
res.send(JSON.stringify(req.query));
});

If you want to get these variables , have access to Deconstruct assignment grammar , Or directly . Get the variables
app.get('/getUser', (req, res) => {
let {
name } = req.query; // tom
let age = req.query.age; // 12
let address = req.query.address; // undefined
console.log(name, age, address);
res.send(req.query);
});
Actually , I used to be curious , If you want to pass an object or array in a parameter, you can only use POST request , Until I saw express Documents —— use GET No problem ! But they all need to use [] Operator
First, arrays , Just use Array name []= Parameter values
// http://localhost:3000/getUser?users[]=tom&users[]=jerry&users[]=dog
app.get('/getList', (req, res) => {
let users = req.query.users
res.end(users);
});

Then the object , need Object name [ Property name ]= Property value
// http://localhost:3000/getUser?tom[name]=tom&tom[age]=12&tom[address]=house
app.get('/tom', (req, res) => {
let tom = req.query.tom;
res.end(tom);
});

obtain Get Path parameter
be familiar with Springboot We all know that we can define parameters in the route , And then use @PathVariable Annotation gets the parameter , stay express No problem in . We need to use : Parameter name Defining parameters
app.get('/user/:id', (req, res) => {
console.log(req.params.id);
res.end(req.params.id);
});

Of course , We can define more than one parameter
app.get('/image/:width/:height', (req, res) => {
console.log(req.params.width);
console.log(req.params.height);
res.end(JSON.stringify(req.params));
});

Let's share it here ~
边栏推荐
- QT connection to Shentong database
- Detectron2 source code reading 4-- registrar construction model
- 127.0.0.1、0.0.0.0和localhost
- Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
- Maxiouassigner of mmdet line by line interpretation
- Pytorch BERT
- Use V-IF with V-for
- 维基媒体基金会公布新商业产品“维基媒体企业”首批客户
- Based on svelte3 X desktop UI component library svelte UI
- Esp32 things (x): other functions
猜你喜欢

Couldn't load this key (openssh ssh-2 private key (old PEM format))

Introduction to the runner of mmcv

12. problem set: process, thread and JNI architecture

Pytorch BERT

Explanation on the use of password profiteering cracking tool Hydra
![[wechat applet] realize applet pull-down refresh and pull-up loading](/img/23/2668a3a36fd46f63732c753fd6f237.jpg)
[wechat applet] realize applet pull-down refresh and pull-up loading

将线程绑定在某个具体的CPU逻辑内核上运行

QT connection to Shentong database

C#访问MongoDB并执行CRUD操作

Talk about how the kotlin process started?
随机推荐
Wechat development tool (applet)
Rew acoustic test (II): offline test
Talk about the job experience of kotlin cooperation process
使用华为性能管理服务,按需配置采样率
icon资源
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Use V-IF with V-for
Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
Icon resources
C#访问SQL Server数据库两种方式的比较(SqlDataReader vs SqlDataAdapter)
Find the number that appears only once in the array
Talk about the kotlin cooperation process and the difference between job and supervisorjob
Dart asynchronous task
Mmdet line by line deltaxywhbboxcoder
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
Interpretation of orientedrcnn papers
Bind threads to run on a specific CPU logical kernel
C # get the current timestamp
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
Rew acoustic test (I): microphone calibration