Node.js - 如何从 html 发送数据来表达 [英] Node.js - How to send data from html to express

查看:33
本文介绍了Node.js - 如何从 html 发送数据来表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是html中的表单示例:

<头><meta charset="utf-8"/><title>CSS3 联系表单</title><身体><div id="联系人"><h1>发送电子邮件</h1><form action="/myaction" method="post"><字段集><label for="name">名称:</label><input type="text" id="name" name="name" placeholder="输入您的全名"/><label for="email">电子邮件:</label><input type="email" id="email" placeholder="输入您的电子邮件地址"/><label for="message">消息:</label><textarea id="message" placeholder="你在想什么?"></textarea><input type="submit" value="发送消息"/></fieldset></表单>

这是在服务器上运行的 node.js 函数:

var sys = require('sys'),http = require('http');http.createServer(function (req, res) {开关 (req.url)案例/我的行动":重发(?????);休息;}}).听(8080);sys.puts('服务器运行在 http://127.0.0.1:8080/');

我有两个问题:

  1. 如何从 html 页面调用 node.js 中的 myaction 函数?因为 html 文件在 80 端口和 8080 端口上运行 node.js(当我尝试将 node.js 移动到端口 80 时,它会写入//Unhandled 'error' event")
  2. 在 node.js 函数中,我放置了?????"如何从 html 表单中获取数据.当我输入 req.html.body.name 我没有得到数据...

解决方案

使用 http.createServer 是非常低级的,对于按原样创建 Web 应用程序真的没有用.

在它之上使用的一个很好的框架是 Express,我强烈建议使用它.您可以使用 npm install express 安装它.

如果有,您可以创建一个基本应用程序来处理您的表单:

var express = require('express');var bodyParser = require('body-parser');var app = express();//注意,在 express 版本 4 中,express.bodyParser() 是//不推荐使用单独的body-parser"模块.app.use(bodyParser.urlencoded({extended: true }));//app.use(express.bodyParser());app.post('/myaction', function(req, res) {res.send('你发送的名字是 "' + req.body.name + '".');});app.listen(8080,函数(){console.log('服务器运行在 http://127.0.0.1:8080/');});

您可以使用以下方法使表单指向它:

您不能在端口 80 上运行 Node 的原因是该端口上已经有一个进程在运行(它正在为您的 index.html 提供服务).您还可以使用 Express 来提供静态内容,例如 index.html,使用 express.static 中间件.

this is form example in html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS3 Contact Form</title>
</head>
<body>
<div id="contact">
    <h1>Send an email</h1>
    <form action="/myaction" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" placeholder="Enter your full name" />

            <label for="email">Email:</label>
            <input type="email" id="email" placeholder="Enter your email address" />

            <label for="message">Message:</label>
            <textarea id="message" placeholder="What's on your mind?"></textarea>

            <input type="submit" value="Send message" />

        </fieldset>
    </form>
</div>
</body>
</html>

and this is node.js function that run on the server:

var sys = require('sys'),
    http = require('http');
    http.createServer(function (req, res) {
            switch (req.url) 
                case '/myaction':
                        res.end(?????);
                    break;
            }
    }).listen(8080);
sys.puts('Server running at http://127.0.0.1:8080/');

I have 2 questions:

  1. How can I call myaction function in the node.js from the html page? Because the html file runs on port 80 and node.js on 8080 (when I try to move the node.js to port 80 it writes "// Unhandled 'error' event")
  2. In the node.js function where I put "?????" how can I get data from the html form. When I type req.html.body.name I don't get the data...

解决方案

Using http.createServer is very low-level and really not useful for creating web applications as-is.

A good framework to use on top of it is Express, and I would seriously suggest using it. You can install it using npm install express.

When you have, you can create a basic application to handle your form:

var express = require('express');
var bodyParser = require('body-parser');
var app     = express();

//Note that in version 4 of express, express.bodyParser() was
//deprecated in favor of a separate 'body-parser' module.
app.use(bodyParser.urlencoded({ extended: true })); 

//app.use(express.bodyParser());

app.post('/myaction', function(req, res) {
  res.send('You sent the name "' + req.body.name + '".');
});

app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
});

You can make your form point to it using:

<form action="http://127.0.0.1:8080/myaction" method="post">

The reason you can't run Node on port 80 is because there's already a process running on that port (which is serving your index.html). You could use Express to also serve static content, like index.html, using the express.static middleware.

这篇关于Node.js - 如何从 html 发送数据来表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆