Ken428965 2023-01-31 20:09 采纳率: 53.9%
浏览 49
已结题

node,powershell里显示name未定义?

在代码编辑器界面里显示有app.js和form.html两个文件:

img

app.js如下:

const http = require('http');
const app = http.createServer();
const url = require('url');
app.on('request', (req, res) => {
    // console.log(req.method);
    // console.log(req.url);
    // console.log(req.headers['accept']);
    res.writeHead(200, {
        'content-type': 'text/html;charset=utf8',
        'hello': 'world'
    });
    // console.log(req.url);
    let { query, pathname } = url.parse(req.url, true).query;
    console.log(url.parse(req.url, true).query);
    console.log(query.name);
    console.log(query.age);
    if (pathname == '/index' || pathname == '/') {
        res.write('

欢迎来到首页

'
); } else if (pathname == '/list') { res.write('welcome to listpage'); } else { res.write('not found'); } if (req.method == 'GET') { res.end('get') } else if (req.method == 'POST') { res.end('post') } // res.end(' hello user '); }); app.listen(3000); console.log('网站服务器启动成功');

form.html如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method="post" action="http://localhost:3000">
        <input type="submit" name="">
    </form>
</body>
</html>
 

已成功启动网站服务器:

img

网上的教程说, 在powershell里,console.log(query.name) 和 console.log(query.age) 会显示出zhangsan和20这两个值:

img

然而,我按照教程实施出来的效果则如下,显示name未定义,与教程显示的有出入,且chrome浏览器显示无法访问此网站::

img

img

请问代码是否有写得不对的地方?如何解决?恳请在现有基础上展示说明

  • 写回答

2条回答 默认 最新

  • 木卯彳亍 2023-02-01 09:31
    关注

    去掉这个query

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月1日
  • 已采纳回答 2月1日
  • 创建了问题 1月31日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效