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.json
File will be created automatically
Explanation: It npm
is node.js
a package management tool under the platform
2. Install express
npm i express
It will be created automatically after installation node_modules文件夹
,package-lock.json文件
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)
(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
open the Web page
127.0.0.1:8000
View F12 to
view the most basic request messages and response messages
And response body