我正在尝试通过将表单从页面“ index.html ”提交到“ Page2.html ”来发送参数,从而将请求通过节点服务器.js
这是我的index.html代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
</script>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="Page1.html?v=1&s=2" method="get">
<input type="submit" value="submit"/>
</form>
</body>
</html>
Server.js:
'use strict';
var http = require('http');
var fs = require('fs');
var urlm = require('url');
//var mymodule = require("./hello.js");
var port = process.env.PORT || 1337;
http.createServer(function (req, res) {
var path = urlm.parse(req.url).pathname;
switch (path) {
case '/':
res.writeHead(200, { 'Content-Type': 'text/html' });
fs.readFile('index.html', function (err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write("<h1>Page Not Found</h1>");
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
}
});
break;
case '/Page1.html':
fs.readFile('pages' + path, function (err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write("<h1>Page Not Found</h1>");
res.write(req.url);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.write();
res.end();
}
});
break;
default:
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write("<h1>Everything is working Fine</h1>");
res.end();
break;
}
}).listen(port);
我要在其中打印参数的Page1.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
var url = new URL(url_string);
var parameter = url.searchParams.get("v");
console.log(parameter);
</script>
</body>
</html>
而且,我的输出是“ localhost:myport / Page1.html?” Page1.html中没有参数且控制台为空,因为我是Node.js的初学者,所以我尽了最大努力来解决它,但是失败了。