Node JS服务器请求使用按钮

Node JS Server Requests using buttons

本文关键字:按钮 请求 JS 服务器 Node      更新时间:2023-09-26

我目前正在为我的Node JS应用程序创建一个菜单,我还没有弄清楚如何在不使用浏览器导航栏的情况下发出服务器请求。

我的目录结构如下:

/application/nodeserver.js
            /screens/index.js
            /screens/other.js
            /screens/notfound.js

我的noserver .js如下

// main
var gv_http = null;
var gv_filestream = null;
var gv_server = null;
// screens
var gv_index = null;
var gv_other = null;
var gv_notfound = null;
// set variables
gv_http = require('http');
gv_filestream = require('fs');
gv_server = gv_http.createServer();
gv_index = require('./screens/index');
gv_other = require('./screens/other');
gv_notfound = require('./screens/notfound');
// server
gv_server.on('request', function(request, response) {
   switch(request.url) {
      case '/':
      case '/index':
         gv_index.gui(response);
         break;
      case '/other':
         gv_other.gui(response);
         break;
      default:
         gv_notfound.gui(response);
         break;
   }
});
gv_server.listen(90);

这一点都很好,但我可以触发我的case语句的唯一方法是在浏览器中输入,它会弹出我的javascript屏幕。

其中一个屏幕index.js

var gv_mainmenu = null;
gv_mainmenu = require('./elements/mainmenu');
var gui = function(response) {
   response.writeHead(200, {'Content-Type':'text/html'});
   response.write(
      '<html>' +
      '<body>'
   );
   response.write(
      '<button id="butt_index">' +
      '   index' +
      '</button>' +
      '<button id="butt_other">' +
      '   other' +
      '</button>'
   );
   response.write(
      '<h4>index</h4>'
   );
   response.write(
      '</body>' +
      '</html>'
   );
   response.end();
}
function sendrequest() {
   console.log("request sent");
}
module.exports.gui = gui;

是否可以使用html按钮来模拟这个?我只在像Express这样的框架中看到过这种情况,但我不想在学习的时候使用其中一个。

p

我知道我的代码对JavaScript专家来说可能看起来很奇怪,但我遵循与我一起工作的人的编码标准,我们主要使用Genero编码,所以我尽量让他们和我自己都熟悉它。

最简单的解决方案是在你的按钮上添加一个锚选项卡,这样它们就可以链接到你的新页面。

'<a href="/index"><button id="butt_index">' +
  '   index' +
  '</button></a>'