Express 创建以及使用
Express 创建:
1、首先需要Node.js的环境…在我另一个文档有写怎么配置Node.js
2、建个文件夹,执行命令:
npm init //应用创建一个 package.json 文件
$ npm install express --save
其他use(为了下面的使用而install):
npm install cors
Express使用:
1. 创建index.js文件
var express = require("express");
var All = express();
var bodyParser = require("body-parser"); //使用插件,用于解析/post
const cors = require("cors");
All.use(cors());
All.use(express.static("static")); //加载项目静态内容
All.use(bodyParser.json());
All.use(bodyParser.urlencoded({ extended: false })); //解析字符
/*查询全部*/
All.get("/SelectAll", function (req, res) {
console.log(req.query); //获取前端ajax传递过来的信息并输出
res.send([
{
MarId: 1,
MarName: "MarName1",
MarLvl: 0,
MarRemark: "备注1",
CreateTime: "CreateTime1",
CreateStaffer: "CreateStaffer1",
},
{
MarId: 2,
MarName: "MarName2",
MarLvl: 0,
MarRemark: "备注2",
CreateTime: "CreateTime2",
CreateStaffer: "CreateStaffer2",
},
{
MarId: 3,
MarName: "MarName3",
MarLvl: 0,
MarRemark: "备注3",
CreateTime: "CreateTime3",
CreateStaffer: "CreateStaffer3",
},
]);
});
const port = 3500;
All.listen(port, () =>
console.log(`Express app listening at http://localhost:${port}`)
);
2. 运行:
node index.js
http://localhost:3500/SelectAll
截图展示结果: