Express 框架

Express 是 Node.js 的一个web服务框架

express 框架

入口文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//app.js
let express = require("express");
let app = express();

app.get("/", (req, res) => {
res.send("hello brown ");
});

app.post("/", (req, res) => {
res.send("POST SOME");
});

let server = app.listen(2333, () => {
console.log("Listen:http://127.0.0.1:2333");
});

头信息

1
2
3
4
5
6
7
8
9
10
11
12
13
app.all("*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true); //告诉客户端可以在HTTP请求中带上Cookie
res.header(
"Access-Control-Allow-Headers",
"Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, " +
"Last-Modified, Cache-Control, Expires, Content-Type, Content-Language, Cache-Control, X-E4M-With,X_FILENAME"
);
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", " 3.2.1");
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

获取参数

GET

1
2
3
app.get("", (req, res) => {
console.log(req.query);
});