
在项目的终端中打开,然后分别输入安装express环境
npm init --yes
npm i express
-------------->
- //1.引入express
- const express = require("express");
- //2.创建应用对象
- const app = express();
- //3.创建路由规则
- // request是对请求报文的封装
- // response是对响应报文的封装
- app.get("/", (request, response) => {
- //设置相应
- response.send("HELLO EXPRESS");
- });
- //4.监听端口启动服务
- app.listen(8000, () => {
- console.log("服务已经启动,8000端口监听中...");
- });
我的是node express.js


- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>AJAX GET 请求title>
- <style>
- .result {
- height: 100px;
- width: 200px;
- border: 1px solid #90b;
- }
- style>
- head>
- <body>
- <button>点击发送请求button>
- <div class="result">div>
- body>
- html>

- //1.引入express
- const express = require("express");
- //2.创建应用对象
- const app = express();
- //3.创建路由规则
- // request是对请求报文的封装
- // response是对响应报文的封装
- app.get("/server", (request, response) => {
- //设置响应头 设置允许跨域
- response.setHeader("Access-Control-Allow-Origin" + "*");
- //设置相应
- response.send("HELLO EXPRESS");
- });
- //4.监听端口启动服务
- app.listen(8000, () => {
- console.log("服务已经启动,8000端口监听中...");
- });

- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>AJAX GET 请求title>
- <style>
- .result {
- height: 100px;
- width: 200px;
- border: 1px solid #90b;
- }
- style>
- head>
- <body>
- <button>点击发送请求button>
- <div id="result">div>
- <script>
- //获取button元素
- const btn = document.getElementsByTagName("button")[0];
-
- const result = document.getElementById("result");
-
- //绑定事件
- btn.onclick = function () {
- //1.创建对象
- const xhr = new XMLHttpRequest();
- //2.初始化 设置请求方法和url
- xhr.open("GET", "http://127.0.0.1:8000/server");
- //3.发送
- xhr.send();
- //4.事件绑定处理服务端返回的结果
- // on 相当于when 当....时候
- // readystate是 xhr 对象中的属性,表示状态0 1 2 3 4
- // change改变
- xhr.onreadystatechange = function () {
- //判断(服务端返回了所有的结果,等于4正好结束)
- if (xhr.readyState === 4) {
- //判断响应状态码 200 404 403 401 500
- //2xx成功(以2打头的就算成功)
- if (xhr.status >= 200 && xhr.status < 300) {
- //处理结果 行 头 空行 体
- // console.log(xhr.status); //状态码
- // console.log(xhr.statusText); //状态字符串
- // console.log(xhr.getAllResponseHeaders()); //所有响应头
- // console.log(xhr.response); //响应体
- result.innerHTML = xhr.response;
- }
- }
- };
- };
- script>
- body>
- html>

![]()
和上面写的一样,就是把GET改成POST
xhr.open("POST", "http://127.0.0.1:8000/server");
会发现报错,因为没有和POST匹配

只需要加一段就解决
- app.post("/server", (request, response) => {
- //设置响应头 设置允许跨域
- response.setHeader("Access-Control-Allow-Origin", "*");
- //设置响应
- response.send("HELLO AJAX POST");
- });


我们需要添加一段

//设置请求头
xhr.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
--------------------------------------->

//自定义
xhr.setRequestHeader("name", "liujiayi");



完整代码
- //1.引入express
- const express = require("express");
- //2.创建应用对象
- const app = express();
- //3.创建路由规则
- // request是对请求报文的封装
- // response是对响应报文的封装
- app.get("/server", (request, response) => {
- //设置响应头 设置允许跨域
- response.setHeader("Access-Control-Allow-Origin", "*");
- //设置响应
- response.send("HELLO AJAX");
- });
- app.all("/server", (request, response) => {
- //设置响应头 设置允许跨域
- response.setHeader("Access-Control-Allow-Origin", "*");
- //响应头
- response.setHeader("Access-Control-Allow-Headers", "*");
- //设置响应
- response.send("HELLO AJAX POST");
- });
- //4.监听端口启动服务
- app.listen(8000, () => {
- console.log("服务已经启动,8000端口监听中...");
- });
因为改的地方有点多
- //1.引入express
- const express = require("express");
- //2.创建应用对象
- const app = express();
- //3.创建路由规则
- // request是对请求报文的封装
- // response是对响应报文的封装
- app.all("/json-server", (request, response) => {
- //设置响应头 设置允许跨域
- response.setHeader("Access-Control-Allow-Origin", "*");
- //响应头
- response.setHeader("Access-Control-Allow-Headers", "*");
- //响应一个数据
- const data = {
- name: "liujiayi",
- };
- //对对象进行字符串转化
- let str = JSON.stringify(data);
- //设置响应
- response.send(str);
- });
- //4.监听端口启动服务
- app.listen(8000, () => {
- console.log("服务已经启动,8000端口监听中...");
- });
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>AJAX POST 请求title>
- <style>
- #result {
- width: 200px;
- height: 100px;
- border: 1px solid black;
- }
- style>
- head>
- <body>
- <div id="result">div>
- <script>
- const result = document.getElementById("result");
- result.addEventListener("mouseover", function () {
- //1.获取元素对象
- const xhr = new XMLHttpRequest();
- //设置响应体数据的类型
- xhr.responseType = "json";
- //2.初始化 设置请求方法和url
- xhr.open("POST", "http://127.0.0.1:8000/json-server");
-
- //设置请求头
- xhr.setRequestHeader(
- "Content-Type",
- "application/x-www-form-urlencoded"
- );
- //自定义
- xhr.setRequestHeader("name", "liujiayi");
-
- //3.发送
- xhr.send();
- //4.事件绑定处理服务端返回的结果
- // on 相当于when 当....时候
- // readystate是 xhr 对象中的属性,表示状态0 1 2 3 4
- // change改变
- xhr.onreadystatechange = function () {
- //判断(服务端返回了所有的结果,等于4正好结束)
- if (xhr.readyState === 4) {
- //判断响应状态码 200 404 403 401 500
- //2xx成功(以2打头的就算成功)
- if (xhr.status >= 200 && xhr.status < 300) {
- console.log(xhr.response);
- result.innerHTML = xhr.response.name;
- }
- }
- };
- });
- script>
- body>
- html>

学到这,相信大家对每次对js文件进行微小的调整后,都要重新保存,再运行,十分麻烦。
现在介绍一个工具nodemon
npm install -g nodemon
npx nodemon express.js