Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js从html输入值_Javascript_Html_Node.js - Fatal编程技术网

Javascript node.js从html输入值

Javascript node.js从html输入值,javascript,html,node.js,Javascript,Html,Node.js,我目前正在node.js中制作一个应用程序,希望找到一种从html获取输入的方法。目前,我需要一个下拉菜单来调用函数,并使用该选项作为函数的输入。然而,当我尝试调用一个基本函数(比如console.log('helloworld');)时,什么也没发生。我使用http模块创建一个服务器,并使用另一个函数将html返回给本地主机上的用户。这是我的一段代码 const http = require('http'); http.createServer(function (req, res) {

我目前正在node.js中制作一个应用程序,希望找到一种从html获取输入的方法。目前,我需要一个下拉菜单来调用函数,并使用该选项作为函数的输入。然而,当我尝试调用一个基本函数(比如console.log('helloworld');)时,什么也没发生。我使用http模块创建一个服务器,并使用另一个函数将html返回给本地主机上的用户。这是我的一段代码

const http = require('http');

http.createServer(function (req, res) {
    var html = buildGreeter(req);
    res.writeHead(200, {
        'Content-Type': 'text/html',
        'Content-Length': html.length,
        'Expires': new Date().toUTCString()
    });
    res.end(html);
}).listen(8080);
function buildGreeter(req) {
var test = 'Test test test   ';
var input = '<select onchange = "myFunc()"> <option> foo </option> <option> bar </option> </select>';
return '<!DOCTYPE html> <html><header></header><body>' + test + input + '</body></html>';
}
function myFunc(variable){
    console.log(variable);
}
consthttp=require('http');
http.createServer(函数(req,res){
var html=建筑迎宾员(req);
文书标题(200{
“内容类型”:“文本/html”,
“内容长度”:html.Length,
“过期”:新日期().ToutString()
});
res.end(html);
}).听(8080);
函数构建器(req){
var测试=‘测试’;
变量输入='foo bar';
返回“”+测试+输入+“”;
}
函数myFunc(变量){
console.log(变量);
}
我应该使用另一个模块吗?我知道很多在线示例都将数据发送到服务器(即通过Ajax),但由于我没有用于此项目的服务器,我不知道该解决方案的适用性如何

提前谢谢你的帮助

我将使用节点模块,该模块可通过输入
npm I express
安装。下面是一个使用express设置服务器的示例,其中还实现了一些方法:

const express = require('express')
const renderHtml = require('./util').renderHtml // Render HTML function

let app = express() // Get instance of express server

app.get('/', renderHtml('index.html')) // Home page
app.get('/other', renderHtml('other.html') // Other page

const port = process.env.PORT || 3000
app.listen(port, function() {
  console.log(`Starting server on port ${port}`)
}
在另一个文件中(我将其命名为
util.js
),您可以编写在服务器上调用某些路径时想要执行的函数。例如:

// Render any html file that I have in my './html' directory
exports.renderHtml = function (htmlFileName) {
  return function (req, res) { 
    res.sendFile(`./html/${htmlFileName}`)
  }
}

您可以像中那样编写一个下拉列表,然后让onChange操作成为对您在服务器中指定的另一个页面的调用(例如,请参阅第一个代码块)。

您有一个服务器—您的nodejs应用程序。它在本地主机上运行这一事实并没有使它减少服务器的功能。只是地址不同而已。因此,请随意采用您认为合适的任何客户机-服务器解决方案。我尝试了一下,但它告诉我renderHtml没有在第四行定义,很可能在第五行也没有定义。是否有必须安装的模块?如果没有,我错过了什么?很抱歉。变量util应该是renderHtml。我在回答中修复了它。此外,我没有澄清,但您必须安装express node模块