1.代码
1.1服务代码
//npm init --yes
//npm i express
//引入express
const express = require("express");
//创建应用对象
const app = express();
//创建路由规则
//request:是对请求报文的封装
//response:是对响应报文的封装
app.get("/server", (request, response) => {
//设置响应头:设置允许跨域
response.setHeader('Access-Control-Allow-Origin','*');
//设置响应体
response.send("HELLO EXPRESS");
});
//监听端口启动服务
app.listen(8000, () => {
console.log("服务已经启动,8000端口监听中……");
});
1.2前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">