node使用express搭建简易本地服务器
1、首先安装node.js,node官网https://nodejs.org/en/
2、npm init 创建 package.json 文件 一路回车
3、安装express npm install express --save 生成package-lock.json文件。
4、引入express并使用如下方式
var express = require('express');
var app = express();
5、创建index.js文件 复制一下代码。
设置请求头
app.all('*', function (req, res, next) {
if (req.path !== "/" && !req.path.includes(".")) {
res.header('"Access-Control-Allow-Credentials", true');
res.header("Access-Control-Allow-Origin", req.headers["origin"] || "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Content-Type", "application/json;charset=utf-8");
}
next();
});
配置端口,引入文件
app.use("/clinicMedicalRecord", (req, res) => {
res.json(clinicMedicalRecord);
});
app.listen(3000, () => {
console.log("api-server is ready on port 3000")
})
const clinicMedicalRecord = {
"patientDetail": {
"name": "郭俊明",
"sex": "女",
"age": null,
"patientId": "123456777",
"patientDepar": "心内门诊",
"patientDate": "2021-01-28",
"patientSpringWaterNum":'65465456465',
"mainSuit":"主诉0000",
"nowHistory": "现病史111",
"pastHistory": "既往史2222",
"familyIll": "家族史3333",
"individual": "个人史44444",
"obstericalHistory": "婚孕史55555",
"menses": "月经史66666",
"checkBody": "体格检查77777",
"adcive": "医生嘱托8888"
}
}
6、打开一个终端 输入node index.js运行 , 控制台会提示 api-server is ready on port 3000
7、http://localhost:3000/clinicMedicalRecord 打开自己的请求地址,就会有数据。
如有问题请留言,一起沟通。