Node JS在表单提交时出现无响应

3

我刚开始学习node.js,我想知道为什么当我提交表单时,我的小表单会停止。这是我的代码:

var sys = require('sys'),
    http = require('http');

    http.createServer(function (req, res) {
            switch (req.url) {
                case '/':
                    res.writeHead(200, {'Content-type': 'text/html'});
                    res.end(
                        '<form action="/myaction" method="post" enctype="multipart/form-data">'+
                        '<input type="text" name="field1">' +
                        '<input type="text" name="field2">' +
                        '<input type="submit" value="Submit">' +
                        '</form>'
                    );
                    break;
                case '/myaction':
                    res.writeHead(200, {'Content-type': 'text/html'});
                    sys.puts('Hello');
                    /*
                    if (req.method == 'POST') {
                        req.on('data', function(chunk){
                            res.writeHead(200, chunk.toString());
                        });
                    }
                    */
                    break;
            }
    }).listen(8080);

sys.puts('Server running at http://127.0.0.1:8080/');

一旦我点击提交按钮,表单会连接到/myaction,但从未显示。我知道它已连接,因为我在终端看到了“Hello”文本。然而,在浏览器上我看到的是:
此网页不可用。
网页http://127.0.0.1:8080/myaction可能暂时无法使用,或者已永久移动到新网址。
有人能说明问题出在哪里吗?
2个回答

1

我认为你应该真正使用express来帮助你简化Web开发。在这里,你可以观看创作者的一个小screencast,进行简短的介绍。


@Thorpe express/connect 是前进的道路!此外,nodetuts.com 上有很多关于 express.js/connect 的视频教程,你可以观看。 - Alfred

1

在'/myaction' -case中,您没有正确结束响应。请在那里使用res.end()。


我在测试时刚得到了答案。 :) - Teej
Epeli,您知道如何从我提交的表单中获取数据吗? - Teej
1
@Thorpe Obaze从数据事件中获取。示例:http://phizcode.blogspot.com/2010/03/beyod-static-nodejs-and-post-method.html - esamatti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接