当前位置:网站首页>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. !!!!
边栏推荐
- Applet learning path 2 - event binding
- About MySQL Boolean and tinyint (1)
- 8.8 heap insertion and deletion
- Esp32 (IX): OTA function of function development
- [paid promotion] collection of frequently asked questions, FAQ of recommended list
- Talking about kotlin process exception handling mechanism
- List set export excel table
- 100 lines of code and a voice conversation assistant
- CUDA realizes L2 European distance
- What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
猜你喜欢

基于Svelte3.x桌面端UI组件库Svelte UI

Flink SQL custom connector

Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function

Implementing custom drawer component in quick application

Detailed explanation of pipline of mmdetection

Esp32 things (3): overview of the overall system design

4. use ibinder interface flexibly for short-range communication

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

The elegant combination of walle and Jianbao

ES6 learning path (II) let & const
随机推荐
Sort (simple description)
Find the number that appears only once in the array
Row column (vertical and horizontal table) conversion of SQL
关于Lombok的@Data注解
Esp32 things (3): overview of the overall system design
[cmake] make command cannot be executed normally
CUDA realizes matrix multiplication
Couldn't load this key (openssh ssh-2 private key (old PEM format))
[protobuf] protobuf generates cc/h file through proto file
Abstract factory pattern
Detectron2 source code reading 3-- encapsulating dataset with mapper
Deep understanding of kotlin collaboration context coroutinecontext
C#访问SQL Server数据库两种方式的比较(SqlDataReader vs SqlDataAdapter)
Esp32 (4): overview of the overall code architecture
Flink sql -- No factory implements ‘org. apache. flink. table. delegation. ExecutorFactory‘.
ES6 learning path (IV) operator extension
Treatment process record of Union Medical College Hospital (Dongdan hospital area)
Dart basic notes
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Use V-IF with V-for