如何从HTML登录页面在node.js的控制台中进行通信或获取数据?

问题描述 投票:1回答:1

我正在创建一个JavaScript格式的服务器。它会侦听特定的端口。当我运行它时,它从特定的URL加载一个HTML页面。 HTML页面有用户名和密码框和提交按钮。

当我从任何浏览器使用相同的端口打开该URL时,只有当我单击“提交”按钮时,该页面才会成功打开。比用户名和密码的填充值,我需要在我的服务器端。

在这里,我附上服务器和登录页面代码。

实际上,我是HTML和JavaScript的新手。但是,我尝试使用此功能,但没有得到HTML按钮事件的响应。

LoginServer.js

#!/usr/bin/env node
"use strict";

var port = 9090,              // Port on which to listen
    http = require('http'),   // Call http lib
    url  = require('url'),
    fs   = require('fs'),
    b    = require('bonescript');

// (1) Create server using http lib
var server = http.createServer(servePage);  

// (2) Listen on port
server.listen(port);                        

console.log("@ Listening on : "+port);

// (3) Call below function when request come
function servePage(req, res) 
{

 // (4) Get path for requested file 
 var path = url.parse(req.url).pathname;
 console.log("@ Request came for path : "+path);

  // (5) Read the requested file content from file system
 fs.readFile(__dirname + path, function (err, data) 
 {

  // If found any ERROR then call given function
  if (err) 
  {                                      
   return send404(res); 
  }

  // Write down html code at end.
  else
  {  
   res.write(data, 'utf8');                        
   res.end();
  } 
 });

}

function send404(res) 
{
 res.writeHead(404);
 res.write('404 - page not found');
 res.end();
}


的login.html

<html>

<!-- Set TAB Title -->
<head> <title> RxLogin </title> </head>

<body>

<form name="Login" action="http://192.168.7.2:9090" method="get">

<table>

<tr> <th> Username </th> <th> <input type="text" name="usrnm" ID="textbox_1"/> </th> </tr>
<tr> <th> Password </th> <th> <input type="password" name="passwd"/> </th> </tr>
<tr> <th> <input type="button" value="Login"/> </th>  </tr>

</table>

</form>

</body>

</html>

当按下提交按钮时,我需要解决方案从服务器端的HTML登录页面的凭据数据中获取数据。

所以,最后我想要“LoginServer.js”文件中的代码。我还想在“Login.html”文件中提交按钮点击事件代码。

javascript html node.js html5
1个回答
0
投票

您需要将表单发送到您的服务器,我建议您查看:https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript

希望能帮助到你

© www.soinside.com 2019 - 2024. All rights reserved.