当前位置:网站首页>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 ~
边栏推荐
- Interpretation of orientedrcnn papers
- Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
- 127.0.0.1、0.0.0.0和localhost
- 9.JNI_ Necessary optimization design
- Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu
- 关于Lombok的@Data注解
- Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
- Rew acoustic test (V): equipment required for test
- Harmonyos actual combat - ten thousand words long article understanding service card development process
- List set export excel table
猜你喜欢

Duplicate entry '2' for key 'primary appears in JPA‘

Set, map and modularity

Talking about kotlin process exception handling mechanism

Abstract factory pattern

The elegant combination of walle and Jianbao

Resnet50+fpn for mmdet line by line code interpretation

Sort (simple description)

Advanced technology management -- how managers design and build echelons

Pit encountered by fastjason

Interpretation of source code demand:a rotation equivariant detector for aerial object detection
随机推荐
将线程绑定在某个具体的CPU逻辑内核上运行
Unsupportedclassversionerror is reported when starting jar package. How to repair it
CUDA implements matrix replication
Mmdet line by line deltaxywhbboxcoder
Deep understanding of kotlin collaboration context coroutinecontext
Why must redis exist in distributed systems?
基于Svelte3.x桌面端UI组件库Svelte UI
【付费推广】常见问题合集,推荐榜单FAQ
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Baidu map JS browsing terminal
[data analysis and display]
Detailed explanation of rect class
Common query and aggregation of ES
Mmcv expanding CUDA operator beginner level chapter
Comparaison de deux façons d'accéder à la base de données SQL Server (sqldatareader vs sqldataadapter)
Esp32 things (VIII): music playing function of function development
12. problem set: process, thread and JNI architecture
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
Abstract factory pattern
[paid promotion] collection of frequently asked questions, FAQ of recommended list