当前位置:网站首页>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. !!!!
边栏推荐
- Explanation on the use of password profiteering cracking tool Hydra
- Coredata acquisition in swift sorting, ascending, descending
- Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
- 4. use ibinder interface flexibly for short-range communication
- Use of Baidu face recognition API
- Flink Exception -- No ExecutorFactory found to execute the application
- 127.0.0.1, 0.0.0.0 and localhost
- So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
- Bottomsheetbehavior principle of realizing the home page effect of Gaode map
- Understanding society at the age of 14 - reading notes on "happiness at work"
猜你喜欢
ES6 learning path (III) deconstruction assignment
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
[data analysis and display]
Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"
Detailed explanation of pytoch's scatter function
Anchorgenerator for mmdet line by line interpretation
Based on svelte3 X desktop UI component library svelte UI
Bind threads to run on a specific CPU logical kernel
将线程绑定在某个具体的CPU逻辑内核上运行
随机推荐
Evaluation standard for audio signal quality of intelligent speakers
Deep Learning with Pytorch- A 60 Minute Blitz
Maxiouassigner of mmdet line by line interpretation
QT connection to Shentong database
Use Huawei performance management service to configure the sampling rate on demand
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Esp32 things (x): other functions
Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
2020-11-02
证券开户的优惠怎样才能得到?在线开户安全?
Detectron2 source code reading 4-- registrar construction model
启动jar包报错UnsupportedClassVersionError,如何修复
Common query and aggregation of ES
How can I get the discount for opening a securities account? Is online account opening safe?
How to format an UTC date to use the Z (Zulu) zone designator in php?
Introduction to the runner of mmcv
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
Esp32 (7): I2S and I2C drivers for function development