发送html表单数据到节点js [英] sending html form data to node js

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

问题描述

我有以下代码

    <!--index.html-->

  <form id = "lang" action="/myform" method="POST" >
    <input type="text" name="mytext" required />
    <input type="submit" value="Submit" />
</form>

//index.js

  var fileServer = new(nodeStatic.Server)();

  var app = http.createServer( function(req, res){

  fileServer.serve(req, res); 

}).listen(port);

var io = socketIO.listen(app);

io.sockets.on('connection', function(socket) {

  console.log('recieved connection ');
  // convenience function to log server messages on the client

如何将ID为"lang"的文本框中的数据发送到index.js并将其存储在某个变量中?

通过将express作为参数放置在http.createServer()中并在回调中执行filsServer.serve(req, res)来使用它:

express = require('express');
app2 = express();
http = require('http');

    app2.post('/myform', function(req, res){

    console.log(req.body.mytext); 

  });

var app = http.createServer(app2, function(req, res){

 fileServer.serve(req, res);  

}).listen(8082);

这显然是行不通的,因为我需要先加载index.html页面来填写表单,然后通过执行上述代码,程序甚至在html页面可以加载之前就希望有一些表单数据. /p>

还有其他发送数据的方式吗?

解决方案

我可以使用它.这是代码:

var express = require('express');
var app2 = express(); 
var bodyParser = require("body-parser");
var path = require('path');
var socketIO = require('socket.io');

app2.use(bodyParser.urlencoded({ extended: false }));
app2.use(bodyParser.json());

var app = http.createServer(app2);
`var io = socketIO.listen(app);`

app2.use( express.static(__dirname));

app2.post('/form', function(req, res){

  var lang = req.body.mytext;
  console.log( req.body.mytext);
  res.send(lang);
});

app.listen(8081);

尽管已经使用express创建了服务器,但我仍然需要使用HTTP模块来创建服务器,因为express服务器无法与socket.io模块一起使用.

我曾经使用快递服务器来处理我的静态文件.

I have the following code

    <!--index.html-->

  <form id = "lang" action="/myform" method="POST" >
    <input type="text" name="mytext" required />
    <input type="submit" value="Submit" />
</form>

and

//index.js

  var fileServer = new(nodeStatic.Server)();

  var app = http.createServer( function(req, res){

  fileServer.serve(req, res); 

}).listen(port);

var io = socketIO.listen(app);

io.sockets.on('connection', function(socket) {

  console.log('recieved connection ');
  // convenience function to log server messages on the client

How do I send data in the textbox with id "lang" over to index.js and store it in some variable?

using express by placing it as a parameter in http.createServer() and executing filsServer.serve(req, res) in a callback:

express = require('express');
app2 = express();
http = require('http');

    app2.post('/myform', function(req, res){

    console.log(req.body.mytext); 

  });

var app = http.createServer(app2, function(req, res){

 fileServer.serve(req, res);  

}).listen(8082);

this obviously wouldn't work because I need the index.html page to load to fill in the form to begin with and by executing the code above, the program is expecting some form data even before the html page can load.

Is there another way I can send the data?

解决方案

I got it to work. Here's the code:

var express = require('express');
var app2 = express(); 
var bodyParser = require("body-parser");
var path = require('path');
var socketIO = require('socket.io');

app2.use(bodyParser.urlencoded({ extended: false }));
app2.use(bodyParser.json());

var app = http.createServer(app2);
`var io = socketIO.listen(app);`

app2.use( express.static(__dirname));

app2.post('/form', function(req, res){

  var lang = req.body.mytext;
  console.log( req.body.mytext);
  res.send(lang);
});

app.listen(8081);

Despite having created a server using express, I still needed to create the server using the HTTP module because an express server doesn't work with the socket.io module.

And I had used the express server to take care of my static files.

这篇关于发送html表单数据到节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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