2016-02-22 61 views
-3

我使用node.js創建服務器。然後我在特定的地址上構建一個http表單。 (我要在不同的地址上做不同的表格)。 我想從特定字段的用戶接收數據。 (我會給他們不同的ID)。node.js如何從表格接收值

但我無法收到document.getElementById(),因爲DOM在node.js中未定義。

你能否建議一個特定的模塊來解決這個問題,或者一些有用的方法?

var server = new http.Server(function(req, res){ 
if (req.url=='/') { 
     res.statusCode=200; 
     auth(res); 
     res.end(); 
} else { 
     res.statusCode=404; 
     res.end("Page not found"); 
     } 

}) 


function auth(res) { 

    res.writeHead(200, { 
     'Content-Type': 'text/html', 

    }); 

    var body = ''; 

    body= '<form action="/" method="post">'+ 
     '<thead>Connection details </thead>' + 
     '<br>'+ 
     '<textarea id ="text" name="text" rows="1" cols="50"></textarea><br>'+ 
     '<input value="localhost" id="host">Host</input><br>' + 
     '<input value="root" id="user">User </input><br>' + 
     '<input value="********" id="pass">Pasword </input><br>' + 
     '<input type="submit" value="Connect" id="scheme"></input><br></body></html>' 

    var toWrite = header + body; 

    res.write(toWrite); 
} 

回答

1

設置爲你的項目現在,您將收到req.bodyform的參數。我建議查看expressbody-parser,以解析傳入的數據。

+0

謝謝。 對我可怕的英語感到抱歉。 –