# 六.案例

1.一个简易的服务器

let express = require("express");
let app = express();
app.listen("3000", () => {
  console.log("已经成功连接3000端口");
});

1
2
3
4
5
6

2.get请求

let express = require("express");
let app = express();

app.get("/", (req, res) => {
  res.end("welcome to index");
});

app.get("/about", (req, res) => {
  res.end("welcome to about");
});

app.listen("3000", () => {
  console.log("你已经成功连接3000端口");
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

3.post请求

let bodyParser = require("body-parser");
let express = require("express");
let app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.post("/login", (req, res) => {
  console.log(req.body.username);
});

1
2
3
4
5
6
7
8
9