03_param和post请求

const express = require("express")
const app = express()
const path = require("path")

// 配置静态资源的路径
// public http://localhost:3000/
app.use(express.static(path.resolve(__dirname, "public")))
// 引入解析请求体的中间件
app.use(express.urlencoded())

// 添加一个路由,可以读取get请求的参数
// /login -->  http://localhost:3000/login
app.get("/login", (req, res) => {
    // 获取用户发送的数据
    // 通过req.query来获取查询字符串中的数据
    if (req.query.username === "admin" && req.query.password === "123123") {
        res.send("<h1>欢迎您回来!登录成功</h1>")
    } else {
        res.send("<h1>用户名或密码错误!</h1>")
    }
})

app.post("/login", (req, res) => {
    // 通过req.body来获取post请求的参数(请求体中的参数)
    // 默认情况下express不会自动解析请求体,需要通过中间件来为其增加功能
    // console.log(req.body)
    const username = req.body.username
    const password = req.body.password

    if (username === "admin" && password === "123123") {
        res.send("<h1>登录成功</h1>")
    } else {
        res.send("<h1>登录失败</h1>")
    }
})

// get请求发送参数的第二种方式
// /hello/:id 表示当用户访问 /hello/xxx 时就会触发
// 在路径中以冒号命名的部分我们称为param,在get请求它可以被解析为请求参数
// param传参一般不会传递特别复杂的参数
// app.get("/hello/:name/:age/:gender", (req, res) => {
app.get("/hello/:name", (req, res) => {
    // 约定由于配置

    // 可以通过req.params属性来获取这些参数
    console.log(req.params)

    res.send("<h1>这是hello路由</h1>")
})

app.listen(3000, () => {
    console.log("服务器已经启动~")
})

// 配置静态资源的路径

// public http://localhost:3000

post请求

 // 通过req.body来获取post请求的参数(请求体中的参数)

    // 默认情况下express不会自动解析请求体,需要通过中间件来为其增加功能

需要引入解析请求体的中间件

app.use(express.urlencoded())

get请求

 get请求发送参数的第二种方式

 /hello/:id 表示当用户访问 /hello/xxx 时就会触发

 在路径中以冒号命名的部分我们称为param,在get请求它可以被解析为请求参数

 param传参一般不会传递特别复杂的参数

app.get("/hello/:name/:age/:gender", (req, res) => {

   可以通过req.params属性来获取这些参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 的 HttpURLConnection 或者 Apache HttpClient 库来进行 POST 请求,示例代码如下: 使用 HttpURLConnection: ``` import java.net.HttpURLConnection; import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { URL url = new URL("http://example.com/api.php"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setDoOutput(true); String postData = "param1=value1&param2=value2"; OutputStream os = con.getOutputStream(); os.write(postData.getBytes()); os.flush(); os.close(); int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } } ``` 使用 Apache HttpClient: ``` import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost postRequest = new HttpPost("http://example.com/api.php"); StringEntity input = new StringEntity("{\"param1\":\"value1\",\"param2\":\"value2\"}"); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String output; while ((output = br.readLine()) != null) { System.out.println(output); } } } ``` 请将 URL 和 postData 替换为你自己的接口地址和 POST 数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值