【发布时间】:2018-10-09 07:33:29
【问题描述】:
我有以下代码
<!--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:
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 页面加载之前就需要一些表单数据。
还有其他方法可以发送数据吗?
【问题讨论】:
标签: javascript html node.js forms