当前位置:网站首页>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. !!!!
边栏推荐
- Influencing factors of echo cancellation for smart speakers
- 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())
- Understanding society at the age of 14 - reading notes on "happiness at work"
- [untitled]
- 100 lines of code and a voice conversation assistant
- What are the SQL add / delete / modify queries?
- [cmake] make command cannot be executed normally
- Dart basic notes
- [data analysis and display]
- Rew acoustic test (I): microphone calibration
猜你喜欢
Detailed explanation of pipline of mmdetection
Esp32 (4): overview of the overall code architecture
Implementing custom drawer component in quick application
Pytorch BERT
Mmdet line by line deltaxywhbboxcoder
[data analysis and display]
Flink SQL custom connector
Introduction to the runner of mmcv
Detectron2 source code reading 4-- registrar construction model
Dart asynchronous task
随机推荐
Detailed explanation of rect class
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
Get to know handler again
Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
Introduction to the runner of mmcv
[wechat applet] realize applet pull-down refresh and pull-up loading
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
icon资源
Tutorial for beginners of small programs day01
Installation, use and explanation of vulnerability scanning tool OpenVAS
技术管理进阶——管理者如何进行梯队设计及建设
Flutter theme (skin) changes
Detectron2 source code reading 3-- encapsulating dataset with mapper
About Lombok's @data annotation
2020-11-02
Dart asynchronous task
Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
Common query and aggregation of ES
C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
Qt连接神通数据库