Nodejs非常简单Form with Routes无法发布 [英] Nodejs very simple Form with Routes failing to post
问题描述
我在NodeJS / ExpressJS应用程序中有一个超级简单的表单,无法发布。
I have a super simple form within a NodeJS/ExpressJS app which fails to post.
app.js代码(我称之为server.js)看起来像这个。
The app.js code (I call it server.js) looks like this.
var express = require('express');
var fs = require('fs');
var path = require('path');
var urlEncoded = require('urlencoded-request-parser');
var bodyParser = require('body-parser');
var server = express();
var port = process.env.port || 1337;
server.use(urlEncoded());
server.use(bodyParser.urlencoded({extended: true}));
var routePath="./routes/";
fs.readdirSync(routePath).forEach(function(file) {
var route=routePath+file;
require(route)(server);
});
server.set('view engine', 'ejs');
server.set('views', __dirname);
server.listen(port);
我的帖子表格(post.ejs)
My post form (post.ejs)
<form method="post" action="post-put">
<fieldset>
<!-- Form Name -->
<legend>Which Test Type</legend>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-2 control-label" for="TestType">Please test our</label>
<div class="col-md-4">
<label class="radio-inline" for="TestType-0">
<input type="radio" name="SoftwareTestType" id="TestType-0" value="SoftwareTest" checked="checked">
Software
</label>
<label class="radio-inline" for="TestType-1">
<input type="radio" name="HardwareTestType" id="TestType-1" value="HardwareTest">
Hardware
</label>
</div>
</div>
</fieldset>
<input type='submit' class="btn btn-default" id='submit' value='Submit your Test'/>
</form>
结果显示在此页面中(post-results.ejs)
The results are shown in this page (post-results.ejs)
<%= Test %>
我有两条路线(post-get.js)
I have two routes (post-get.js)
module.exports = function(server){
server.get('/post', function(req,res){
res.render('./views/test/post.ejs',{
});
});
}
和(post-puts.js)
and (post-puts.js)
module.exports = function(server){
server.post('post-put', function(req,res){
console.log("In Submission POST")
var TestType = "";
if(req.body.HardwareTestType == true)
TestType = "Hardware";
else
TestType = "Software";
res.render('./views/test/post-results.ejs',{
Test: TestType
});
});
}
当我点击提交按钮时,开发者工具会显示:
When I click on submit button, developer tools shows this:
这让我感动疯。有谁知道如何将结果发布到post-results.ejs文件。应用程序的文件系统布局如下所示:
This is driving me insane. Does anyone know how to get this to post the results to the post-results.ejs file. The layout of the filesystem for the application looks like this:
更新:
表单和路由器都有相同的路径post-puts。
在post-puts路由中有一个console.log()......但它没有被调用。
我认为它仍然可以是图像中显示的应用程序类型两个(开发者工具)
当事物只有1个功能时,说它不起作用是特定的 - 如果我可以更具体我知道问题是什么并修复它。
UPDATE: Both the form and the router have same paths "post-puts". In the post-puts route there is a console.log() ... but it is NOT called. I think it could still be the application type as shown in the image two up (Developer Tools) When a "thing" has only 1 function, saying it does not work is specific - if I could be more specific I would know what the problem was and fix it.
推荐答案
已解决
上述问题的解决方案如下:
The resolution to the above problem was the following.
1)我将路线改回/ post-put在Action和Route文件中。
2)我在表单中添加了enctype =multipart / form-data。
1) I changed the route back to "/post-put" in the form Action and in the Route file. 2) I added enctype="multipart/form-data" to the form.
所以表单现在看起来像
<form method="post" action="/post-put" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Which Test Type</legend>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-2 control-label" for="TestType">Please test our</label>
<div class="col-md-4">
<label class="radio-inline" for="TestType-0">
<input type="radio" name="TestType" id="TestType-0" value="SoftwareTest" checked="checked">
Software
</label>
<label class="radio-inline" for="TestType-1">
<input type="radio" name="TestType" id="TestType-1" value="HardwareTest">
Hardware
</label>
</div>
</div>
</fieldset>
<input type='submit' class="btn btn-default" id='submit' value='Submit your Test'/>
</form>
替代解决方案
将表单输入保持为 enctype =application / x-www-form-urlencoded
在App.js(我的server.js)
删除 app。 use(bodyparser.urlencoded());
更改 app.use(bodyParser.urlencoded({extended:true}));
到 server.use(bodyParser.urlencoded());
现在表单的POST方法有效!
感谢那些试图提供帮助的人。
Now the POST method of the form works! Thanks to those who tried to help.
这篇关于Nodejs非常简单Form with Routes无法发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!