# 二.案例

# 1.package.json

{
  "name": "eg",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 2.一个简易的服务器

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

# 3.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

# 4.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