node js get post 发送数据

var http=require('http');

var ejs=require('ejs');

// console.log(path.extname)
var router=require('./model/router.js');

//console.log(mimeModel.getMime(fs,'.html'));

 var url=require('url');

//根据不同的url 执行不同的业务逻辑


http.createServer(function(req,res){
 
   // router.statics(req,res,'static');
    //login登陆的一些功能  register 注册的功能
    // console.log(req.url);
    res.writeHead(200,{"Content-Type":"text/html;charset:utf-8"});
    var pathname=url.parse(req.url,true).pathname;
   
    var method=req.method.toLocaleLowerCase();
    console.log(method);
   
   
    if(pathname=='/login'){//显示登陆页面
        ejs.renderFile('views/form.ejs',
        {
        },function(err,data){

            res.end(data);
        })


        
     }else if (pathname=='/dologin'&& method=='get'){

        //get 方式获取数据 
        console.log(url.parse(req.url,true).query); 
        res.end('dologin get');
       

    }else if (pathname=='/dologin'&& method=='post'){
        //post方式获取数据
        var postStr='';
        req.on('data',function(chunk){

            postStr+=chunk;
        })
        req.on('end',function(err,chunk){

            // postStr+=chunk;

            res.end("<script>alert('登陆成功');history.back();</script>");
        })


    }
    
    
    
    else{
       
        
        ejs.renderFile('views/index.ejs',
        {
        },function(err,data){
           
            res.end(data);
        })


    }


}).listen(8881);

form.ejs

<!DOCTYPE html>
<html>

<head>
     <meta charset="UTF-8">
    <title></title>
</head>
<body>

<h2>登录</h2>
<form action="/dologin" method="post">
    <input type="text" name="username"/>
    <br/>
    <input type="password" name="password"/>

    <input type="submit" value="登录"/>
</form>


</body>
</html>

login.ejs

<!DOCTYPE html>
<html>

<head>
     <meta charset="UTF-8">
    <title></title>
</head>
<body>

<h2>这是一个ejs的后台模板引擎__登陆</h2>
<h2><%=msg%></h2>
<br/>
<hr/>
<ul>
    <%  for(var i=0;i<list.length;i++){ %>
        <li> <%=list[i]%></li>

        <%  }%>

        
</ul>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以用中文为你讲解如何使用 Node.js 发送 HTTP GET 和 POST 请求。 首先,你需要安装 Node.js 和 npm(Node.js 包管理器)。然后,你可以使用 npm 安装 "request" 模块,这是一个简单易用的 HTTP 客户端库,可以让你轻松发送 HTTP 请求。 安装 "request" 模块的命令如下: ``` npm install request ``` 安装完成后,你就可以在 Node.js 代码中引入 "request" 模块,并使用它发送 HTTP 请求了。 发送 HTTP GET 请求的代码如下: ``` const request = require('request'); request.get('http://www.example.com', (error, response, body) => { if (!error && response.statusCode == 200) { console.log(body); } }); ``` 发送 HTTP POST 请求的代码如下: ``` const request = require('request'); const options = { url: 'http://www.example.com', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, form: { key: 'value', }, }; request(options, (error, response, body) => { if (!error && response.statusCode == 200) { console.log(body); } }); ``` 希望这些信息能帮到你。 ### 回答2: 使用Node.js的http模块可以发送GET和POST请求。 发送GET请求的步骤如下: 1. 首先,我们需要导入http模块,并创建一个服务器对象: ```javascript const http = require('http'); ``` 2. 然后,我们可以使用http模块的get方法发送GET请求: ```javascript const options = { hostname: 'example.com', path: '/api/some-endpoint', method: 'GET' }; const req = http.request(options, (res) => { res.on('data', (data) => { // 处理返回的数据 }); }); req.on('error', (error) => { // 处理请求错误 }); req.end(); ``` 在上面的代码中,我们使用http.request方法创建一个请求对象。我们需要提供一个包含主机名(hostname)、路径(path)和请求方法(method)的选项(options)对象。然后,我们可以通过调用req.end()方法发送请求。当服务器返回数据时,我们可以通过监听'res'对象的'data'事件来处理数据,同时还可以监听'error'事件来处理请求错误。 发送POST请求的步骤类似,只需要设置请求方法为POST,并提供请求体数据。以下是一个发送POST请求的例子: ```javascript const postData = JSON.stringify({ username: 'john', password: 'secret' }); const options = { hostname: 'example.com', path: '/api/login', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } }; const req = http.request(options, (res) => { res.on('data', (data) => { // 处理返回的数据 }); }); req.on('error', (error) => { // 处理请求错误 }); req.write(postData); req.end(); ``` 在上面的例子中,我们设置了请求方法为POST,并提供了请求体数据,即postData。我们还设置了请求头(headers)来指定请求体数据的类型和长度。 以上就是使用Node.js的http模块发送GET和POST请求的基本步骤。 ### 回答3: 使用Node.js中的http模块可以很方便地发送GET和POST请求。 发送GET请求的步骤如下: 1. 首先需要引入http模块:const http = require('http'); 2. 创建一个http请求对象:const options = { hostname: 'www.example.com', // 请求目标的主机名 path: '/api/data', // 请求目标的路径 method: 'GET' // 请求方法为GET }; 3. 发送请求:const req = http.request(options, (res) => { // 处理响应 res.on('data', (data) => { console.log(data.toString()); }); }); req.end(); 发送POST请求的步骤如下: 1. 引入http模块:const http = require('http'); 2. 创建一个http请求对象:const options = { hostname: 'www.example.com', // 请求目标的主机名 path: '/api/data', // 请求目标的路径 method: 'POST', // 请求方法为POST headers: { 'Content-Type': 'application/json' // 请求头设置为JSON格式 } }; 3. 创建一个POST请求体:const postData = JSON.stringify({name: 'John', age: 25}); 4. 发送请求:const req = http.request(options, (res) => { // 处理响应 res.on('data', (data) => { console.log(data.toString()); }); }); req.write(postData); // 将请求体写入请求 req.end(); 以上就是使用Node.js的http模块发送GET和POST请求的简单示例。根据具体需求,可以对请求头和请求体进行更多的定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值