We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GET和POST是最常用的HTTP请求方法,除此之外还有DELETE、HEAD、OPTIONS、PUT、TRACE等,但都很少用到。
我们可以使用Nodejs自带的url和querystring模块处理接收到的GET数据。
首先新建一个带form表单的HTML文件,讲输入的数据提交到服务器地址:
示例代码:/lesson06/form_get.html
<form action="http://localhost:8080/login" method="get"> 用户:<input type="text" name="username"><br/> 密码:<input type="text" name="password"><br/> <input type="submit" value="提交"> </form>
服务端在接收到请求数据时,可以有3种方式处理数据:
示例代码:/lesson06/server.js
const [ pathname, queryStr ] = req.url.split('?') const query = querystring.parse(queryStr) console.log(pathname, query)
const url = new URL(`http://localhost:8080${req.url}`) const { pathname, search } = url const query = querystring.parse(search.substring(1, url.search.length)) console.log(pathname, query)
// parse方法第二个参数若传true,则会直接将解析出的query值转为对象形式,否则它只是字符串形式 const { pathname, query } = url.parse(req.url, true) console.log(pathname, query)
The text was updated successfully, but these errors were encountered:
url.parse已经弃用了
Sorry, something went wrong.
No branches or pull requests
阅读更多系列文章请访问我的GitHub博客,示例代码请访问这里。
常用请求方式
GET和POST是最常用的HTTP请求方法,除此之外还有DELETE、HEAD、OPTIONS、PUT、TRACE等,但都很少用到。
处理GET数据
我们可以使用Nodejs自带的url和querystring模块处理接收到的GET数据。
首先新建一个带form表单的HTML文件,讲输入的数据提交到服务器地址:
服务端在接收到请求数据时,可以有3种方式处理数据:
The text was updated successfully, but these errors were encountered: