开发者

Node JS hangs on Form submission

I am just starting node.js and I'm wondering why my small form is stopping when I have submitted the form. This is the code I have:

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://开发者_JAVA技巧127.0.0.1:8080/');

As soon as I press submit, the form connects to /myaction but never shows it. I know it connects as I see the 'Hello' text in the terminal. However, I see this on the browser:

This webpage is not available.

The webpage at http://127.0.0.1:8080/myaction might be temporarily down or it may have moved permanently to a new web address.

Can anyone give light to what the problem is?


You aren't ending your response correctly on the '/myaction' -case. Use res.end() there also.


I think you should really use express to help you ease the webdevelopment. Here you can watch a small screencast from the creator for a short introduction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜