当前位置:网站首页>Express の Hello World
Express の Hello World
2022-06-30 09:15:00 【The man of Jike will never admit defeat】
Write it down to yourself Express の Hello World
Express
Is based on Node.js
platform , Fast 、 to open up 、 minimalist Web Development framework . If you use Node.js
Development backend , Probably can't leave Express
. Although there are many excellent frameworks , Such as Egg.js appear , Study Express
Understand some basic underlying HTTP
Knowledge is necessary .
Hello World
To use Express
You need to install Express
. Create folder node
, Run the following command to install Express
.
npm install [email protected]
stay node
Create under folder express
Folder , stay express
Create helloworld.js
Here is helloworld.js
The content of
let express = require('express');
let app = express();
app.get('/', function (req, res) {
console.log('hello, world');
res.send('hello, world');
});
app.listen(3000, function () {
console.log('server started.');
});
after , stay express
Run on path node helloworld.js
. And access http://localhost:3000,
understand Hello World
express()
What does the above code mean . I didn't bother to remember the first line
let app = express();
express()
Create a Express
application , This is a result of express
Top level functions exposed by the module . What does that mean , This means that if you change the imported module name to springBoot
Then the code can still run
let springBoot = require('express');
let app = springBoot();
Ha ha ha , This is because express
What the module exposes is a function . Here is node_modules/express/lib/express.js
Code for
/** * Expose `createApplication()`. */
exports = module.exports = createApplication;
get()
app.get('/', function (req, res) {
console.log('hello, world');
res.send('hello, world');
});
get()
Methods will GET
The request is routed to the specified path , And call the specified callback function . The prototype of this method is app.get(path, callback [, callback ...])
. The first parameter is the address of the route , namely GET
The request path for .🤨 You may ask , What I am asking is not http://localhost:3000
Do you ? Why is it in the code /
Well ?
Open the control panel , At a glance . What seems to be visited is http://localhost:3000
, It's actually http://localhost:3000/
get()
The first parameter of the method is path
. This parameter can be a string , It could be regular , It can also be arrays . The default is /
, Represents the root path . Even if you ask for the root path , The first parameter cannot be left blank !
If the user accesses , No path is lost (http://localhost:3000
) Or lose (http://localhost:3000/index
) We all want to go back hello, word
What shall I do? ? Try this
app.get(['/', '/index'], function (req, res) {
console.log('hello, world');
res.send('hello, world');
});
More about regular and so on , You can refer to * here
Then look get()
The second argument to the method callback
Callback function . The callback function receives 2 Parameters (3 Parameters, wait ) this 2 The first parameter is well-known request
and response
. What appears in the code send()
Method means to send HTTP
Respond to .
res.send('hello, world');
What is the third parameter that pops up ?next
express
Modules allow us to request the same , Set different callback functions , These callback functions pass through next
call , Next, let's take the example above . Although the access root path /
and /index
All return the same content , But we do something special like accessing the root path , You can write like this
app.get('/', function (req, res, next) {
console.log(' I am the root path ');
next(); // Next callback function
});
app.get(['/', '/index'], function (req, res) {
console.log('hello, world');
res.send('hello, world');
});
listen()
What this function specifies is express
Port on which the program listens . If the port is ignored or 0
, The operating system will assign any unused port .
You can also specify the host that the city permits to listen to , as follows
app.listen(3000, '127.0.0.1', function () {
console.log('server started.');
});
You can also receive error messages in the callback function . because 3306
Has been used by this computer mysql
Occupy , So startup failed listen EACCES: permission denied 127.0.0.1:3306
app.listen(3306, '127.0.0.1', function (err) {
if (err) {
console.log(' Start exception ', err);
}
console.log('server started.');
});
Okay , This is it. express
Of Hello World The whole thing . The next plan is to write get
The requested part . come on. !!!!
边栏推荐
- Interviewer: do you understand the principle of recyclerview layout animation?
- Influencing factors of echo cancellation for smart speakers
- Bottomsheetbehavior principle of realizing the home page effect of Gaode map
- Summary of common pytoch APIs
- Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
- 【付费推广】常见问题合集,推荐榜单FAQ
- Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
- Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
- Interpretation of source code demand:a rotation equivariant detector for aerial object detection
- 启动jar包报错UnsupportedClassVersionError,如何修复
猜你喜欢
The elegant combination of walle and Jianbao
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Mmdet line by line deltaxywhbboxcoder
asdsadadsad
Duplicate entry '2' for key 'primary appears in JPA‘
Tutorial for beginners of small programs day01
Bind threads to run on a specific CPU logical kernel
7. know JNI and NDK
Evaluation standard for audio signal quality of intelligent speakers
随机推荐
Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
Flink SQL custom connector
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
[data analysis and display]
127.0.0.1, 0.0.0.0 and localhost
CUDA realizes L2 European distance
List set export excel table
Rew acoustic test (V): equipment required for test
Mmdet line by line deltaxywhbboxcoder
Detailed explanation of rect class
Installation, use and explanation of vulnerability scanning tool OpenVAS
Rew acoustic test (I): microphone calibration
C accesses mongodb and performs CRUD operations
Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development
Wechat development tool (applet)
Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())
Applet learning path 2 - event binding
技术管理进阶——管理者如何进行梯队设计及建设
icon资源