Nodejs get传值的问题

Nodejs get传值的问题

访问:127.0.0.1/?a=1&b=2$c=3

var http = require(‘http’); var url = require(‘url’);

http.createServer(function(req, res) { res.writeHead(200, {‘Content-Type’: ‘text/html’}); res.write(‘Node.js’);

var res_data=url.parse(req.url,true); res.write(res_data.query.b);

console.log(res_data.query);//控制端输出

res.end(’’); }).listen(80);

以上是我的代码; 有2个问题 我一直搞不明白

第一个问题 console.log(res_data.query); 这个代码终端会运行会被运行2次 结果为:{ a: ‘1’, b: ‘2’, c: ‘3’ }与{} 为什么会两次啊?

第二个问题 终端既然都输入了res_data.query 是集合 那么我客户端返回求情的参数是应该没有问题的 可是还是有错 res.write(res_data.query.b); 终端显示 参数必须为first argument must be a string or Buffer 字符串 我改成 res.write(res_data.query.b+’’);加一个空字符 强制转化为字符串 就没报错了 为神马啊啊啊


12 回复

Nodejs get传值的问题

问题描述

你遇到了两个关于如何正确处理GET请求参数的问题。以下是你的代码片段:

var http = require('http');
var url = require('url');

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Node.js');

    var res_data = url.parse(req.url, true);
    res.write(res_data.query.b);

    console.log(res_data.query); // 控制台输出
    res.end('');
}).listen(80);

问题一:控制台输出两次

你的代码中,console.log(res_data.query); 输出了两次,一次是 { a: '1', b: '2', c: '3' },另一次是 {}。这通常是因为你的服务器可能接收到两个请求。为了确认这一点,你可以添加一些调试信息来查看请求的详细信息。

解决方案 你可以通过打印 req.url 来查看每次请求的具体内容:

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Node.js');

    var res_data = url.parse(req.url, true);
    res.write(res_data.query.b);

    console.log(`Request URL: ${req.url}`);
    console.log(res_data.query); // 控制台输出

    res.end('');
}).listen(80);

问题二:无法直接写入查询参数

当你尝试直接写入查询参数时,可能会遇到类型错误,提示 first argument must be a string or Buffer。这是因为 res.write() 方法期望一个字符串或缓冲区作为其参数,而 res_data.query.b 可能不是字符串类型。

解决方案 将查询参数强制转换为字符串即可解决这个问题。你可以使用 String() 或者 '' + 操作符来实现:

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Node.js');

    var res_data = url.parse(req.url, true);
    res.write(String(res_data.query.b)); // 或者 res.write('' + res_data.query.b);

    console.log(res_data.query); // 控制台输出
    res.end('');
}).listen(80);

总结

  1. 控制台输出两次:可能是由于服务器接收到多个请求。可以通过打印 req.url 来确认。
  2. 类型错误:确保传递给 res.write() 的参数是一个字符串。可以使用 String()'' + 来进行类型转换。

希望这些解释和解决方案能帮助你更好地理解并解决问题。


第一个问题 解决了 当我们在服务器访问网页时,我们的服务器可能会输出两次“Request received.”。那是因为大部分服务器都会在你访问 http://localhost:8888 /时尝试读取 http://localhost:8888/favicon.ico

我用你的代码运行没错啊!

127.0.0.1/?a=1&b=2$c=3

有$这个???

看终端。 客户端是没错的

好眼里啊,就是哪里有错 但是B的值也应该出来呗 可惜终端还是有错

b = “2$c=3”

因为data.query是一个序列化的字符串,js中字符串是不能通过xx.b获取自身的对象属性的,所以你的data.query.b返回的是一个null 当用res.write()的时候参数必须是string和buffer类型的 你null + “” 是将null强转为"null"

在127.0.0.1/?a=1&b=2$c=3后续的请求如 http://localhost:8888/favicon.ico 会导致res_data.query.c是undefined,res.write(undefined)肯定要报错啊

哈哈 谢谢, 自己都找出答案来了 结果还不知道

再多问一个问题,如何屏蔽访问这个啊 favicon.ico

针对你的两个问题,我将逐一解答,并提供一些示例代码来帮助你理解。

第一个问题

console.log(res_data.query); 输出 {} 的原因可能是 res_data.query 在某些情况下没有被正确地解析或设置。这可能是因为请求路径不完整或者 url.parse 方法的使用方式有误。

示例代码:

const http = require('http');
const url = require('url');

http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html'});
    
    const res_data = url.parse(req.url, true);
    console.log(res_data.query); // 控制台输出 { a: '1', b: '2', c: '3' }

    res.write(res_data.query.b);
    res.end('');
}).listen(80, () => {
    console.log('Server running at http://127.0.0.1/');
});

第二个问题

当你使用 res.write(res_data.query.b) 时,如果 res_data.query.b 不是字符串类型,Node.js 会抛出错误,因为它期望第一个参数是字符串或 Buffer 类型。为了确保 res_data.query.b 是字符串,你可以显式地转换它。

示例代码:

const http = require('http');
const url = require('url');

http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html'});
    
    const res_data = url.parse(req.url, true);
    console.log(res_data.query); // 控制台输出 { a: '1', b: '2', c: '3' }

    // 显式转换为字符串
    res.write(String(res_data.query.b));
    res.end('');
}).listen(80, () => {
    console.log('Server running at http://127.0.0.1/');
});

通过将 res_data.query.b 转换为字符串,可以避免类型错误。希望这些示例代码能帮助你更好地理解并解决问题。

回到顶部