Use express framework to receive and send ajax information

Back to the general list of articles

Express framework
Our ajax needs to send a pair of requests to the server, so we need this server to receive and send

The basic use of Express
Use the terminal to enter the folder ( ajaxtest)

1. Initializationnode.js

npm init --yes

package.jsonFile will be created automatically

Explanation: It npmis node.jsa package management tool under the platform
Insert picture description here

2. Install express

npm i express

It will be created automatically after installation node_modules文件夹,package-lock.json文件
Insert picture description here

3. Create a project folder Create a file
in the folder (ajaxtest) testone文件夹
and testone文件夹create ( express基本使用.js) file (the code below is the written code)

Insert picture description here

(Express basically uses .js)

// 1. 引入express
const express = require('express');

// 2.创建对象
const app = express();

// 3.创建路由规则  里面的形参 request与response   (自己可以随便定义名字的)
//  建议写成  request与response  因为可以见名思意,方便自己看
// request  对请求报文的封装
// responst 对响应报文的封装

app.get('/', (request, response)=>{
    
    
    // 设置响应
    response.send('HELLO EXPRESS express');

});

// 4. 监听端口启动服务
// 这里listen(8000)后面添加了一个回调,用来提示,告诉自己是否监听成功
app.listen(8000, ()=>{
    
    
    console.log("服务已经启动,8000端口监听中......");
});

Start the service
and execute it in the terminal (location-js folder)

node express基本使用.js

Insert picture description hereInsert picture description here

open the Web page
127.0.0.1:8000

View F12 to
view the most basic request messages and response messages

Insert picture description hereAnd response body
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/111940598