简体   繁体   English

node.js从html输入值

[英]node.js inputting values from html

I'm currently making an app in node.js and want to find a way to get input from html. 我目前正在node.js中制作一个应用程序,并希望找到一种从html获取输入的方法。 Currently, I want a drop down menu to call a function and use the choice as input into the function. 当前,我想要一个下拉菜单来调用一个函数,并使用该选择作为函数的输入。 However, when I try calling a basic function (like console.log('hello world');), nothing happens. 但是,当我尝试调用基本函数(例如console.log('hello world');)时,什么也没发生。 I'm using the http module to create a server and another function to return html to the user on a localhost. 我正在使用http模块创建服务器,并使用另一个函数将html返回给本地主机上的用户。 Here's a snippet of my code. 这是我的代码片段。

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);
}

Should I be using another module? 我应该使用其他模块吗? I know that a lot of examples online had the data sent to the server (ie via Ajax), but since I don't have a server for this project I don't know how applicable that solution is. 我知道很多在线示例都将数据发送到服务器(即通过Ajax),但是由于我没有该项目的服务器,因此我不知道该解决方案的适用性。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

I would use the express node module which can be installed by entering npm i express . 我将使用express节点模块,可以通过输入npm i express来安装它。 Here's an example of setting up a server with express with a few methods implemented as well: 这是一个使用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}`)
}

In another file (I've dubbed it util.js ) you could write functions that you wanted to execute when certain paths were called on your server. 在另一个文件(我将其称为util.js )中,您可以编写要在服务器上调用某些路径时要执行的函数。 For example: 例如:

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

You could write a drop down list like in this example and then have the onChange action be a call to another page that you specify in your server (See first code block for example). 您可以像本例中那样编写一个下拉列表,然后使onChange操作成为您在服务器中指定的另一个页面的调用(例如,请参见第一个代码块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM