当前位置:网站首页>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 ?nextexpress 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. !!!!
边栏推荐
- c#获取当前的时间戳
- Detectron2 source code reading 3-- encapsulating dataset with mapper
- Qt连接神通数据库
- Rew acoustic test (IV): test principle of rew
- C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
- 12. problem set: process, thread and JNI architecture
- 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
- Esp32 (4): overview of the overall code architecture
- Invalid update: invalid number of sections. The number of sections contained in the table view after
- Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
猜你喜欢

Talk about how the kotlin process started?

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

Rew acoustic test (II): offline test

Introduction to the runner of mmcv

Mmdet line by line deltaxywhbboxcoder

Flink SQL custom connector

Using appbarlayout to realize secondary ceiling function

C accesses mongodb and performs CRUD operations

Occasionally, Flink data is overstocked, resulting in checkpoint failure

Interpretation of source code demand:a rotation equivariant detector for aerial object detection
随机推荐
Detectron2 source code reading 3-- encapsulating dataset with mapper
Pytorch BERT
Detailed explanation of rect class
Deep understanding of kotlin collaboration context coroutinecontext
ES6 learning path (II) let & const
Abstract factory pattern
[paid promotion] collection of frequently asked questions, FAQ of recommended list
About MySQL Boolean and tinyint (1)
QT downloading files through URL
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Esp32 (4): overview of the overall code architecture
Torchvision loads the weight of RESNET except the full connection layer
Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
Talking about kotlin process exception handling mechanism
CUDA implements matrix replication
Flutter 0001, environment configuration
Dart basic notes
[JPEG] how to compile JPEG turbo library files on different platforms
ES6 learning path (III) deconstruction assignment
Talk about how the kotlin process started?