如何在Node.js中使用表单模块? [英] How to use forms module in Node.js?
本文介绍了如何在Node.js中使用表单模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,使用node.js中的表单模块( https://github.com/caolan/forms )。我在启动页面显示表单时遇到了jade文件错误。任何人都可以帮助解决此问题。
app.js
var http = require('http'),
express = require('express'),
app = express();
var fields = forms.fields,
validators = forms.validators,
widgets = forms.widgets;
var reg_form = forms.create({
username:fields.string({required:true}),
password:fields.password({required:true}),
confirm:fields.password({
required:true,
验证器:[validators.matchField('password')]
}),
email:fields。电子邮件()
});
$ b $ app.configure(function(){
app.set('views',__dirname);
app.set('view engine','jade');
app.use(express.bodyParser());
app.use(app.router);
});
$ b app.get('/',function(req,res){
res.render('page',{
locals:{
title:'填写表格...',
表格:reg_form.toHTML()
}
});
});
app.post('/',function(req,res){
reg_form.handle(req,{
success:function(form){
res .render('page',{
locals:{
title:'Success!'
}
});
},
other:function (form){
res.render('page',{
locals:{
title:'Failed!',
form:form.toHTML()
}
});
}
});
});
page.jade
h1 = title
- if(typeof form!=='undefined')
form(method ='post')
!= form
input(type ='submit')
page.jsont
< html>
< head>
< title>范例表格< / title>
< style>
表格{
border-top:1px虚线#ccc;
width:375px;
}
标签{
position:relative;
width:200px;
top:6px;
}
。必需标签{
font-weight:bold;
}
.field {
border-bottom:1px虚线#ccc;
padding:10px 5px 15px 5px;
职位:亲属;
}
.field输入,.field select,.field textarea {
margin:-20px 0 0 175px;
width:180px;
}
.field textarea {
margin-top:-15px;
}
.field input [type ='checkbox'] {
position:relative;
margin-left:32px;
top:4px;
}
.field fieldset input {
margin:0;
width:20px;
}
.error {
background:#ffcccc;
}
.error_msg {
颜色:红色;
保证金:-15px 0 0 0;
padding:5px 0;
}
input [type ='submit'] {
margin-top:20px;
}
< / style>
< / head>
< body>
< h1>范例表格< / h1>
< form action =method =GET>
{form}
< input type =submit/>
< / form>
< / body>
< / html>
页面出错
ReferenceError:/home/manigandan/workspace/CreatingForm/page.jade:1
> 1 | h1 =标题
2 |
3 | - if(typeof form!=='undefined')
4 | form(method ='post')
title未定义
在eval at< anonymous> (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:176:8)
at /home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:181:
在Object.render(/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:216:14)
在View.engine(/ home / manigandan / workspace / CreatingForm /node/modules/jade/lib/jade.js:243:13)
在View.render(/home/manigandan/workspace/CreatingForm/node_modules/express/lib/view.js:75:8)
在Function.render(/home/manigandan/workspace/CreatingForm/node_modules/express/lib/application.js:504:10)
在ServerResponse.render(/ home / manigandan / workspace / CreatingForm / node_modules / express /lib/response.js:753:7)
at /home/manigandan/workspace/CreatingForm/app.js:34:9
在回调处(/ home / manigandan / workspace / CreatingForm / node_modules / express / lib / router / index.js:161:37)
at param(/home/manigandan/workspace/CreatingForm/node_modules/express/lib/router/index.js:135:11)
表单无关,而是关于如何将参数传递给
render $ ht_lin>解决方案 c $ c>:
app.get('/',function(req,res){
res.render('page',{
title:'填写表单...',
表单:reg_form.toHTML()
});
});
注意额外的 locals
被删除。
In my application im using forms module in node.js(https://github.com/caolan/forms). I'm getting error in jade file while displaying form in the start up page.Can anyone help to fix this issue.
app.js
var http = require('http'),
express = require('express'),
app = express();
var fields = forms.fields,
validators = forms.validators,
widgets = forms.widgets;
var reg_form = forms.create({
username: fields.string({required: true}),
password: fields.password({required: true}),
confirm: fields.password({
required: true,
validators: [validators.matchField('password')]
}),
email: fields.email()
});
app.configure(function () {
app.set('views', __dirname);
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(app.router);
});
app.get('/', function (req, res) {
res.render('page', {
locals: {
title: 'Filling out the form...',
form: reg_form.toHTML()
}
});
});
app.post('/', function (req, res) {
reg_form.handle(req, {
success: function (form) {
res.render('page', {
locals: {
title: 'Success!'
}
});
},
other: function (form) {
res.render('page', {
locals: {
title: 'Failed!',
form: form.toHTML()
}
});
}
});
});
page.jade
h1= title
- if (typeof form !== 'undefined')
form(method='post')
!= form
input(type='submit')
page.jsont
<html>
<head>
<title>Example Form</title>
<style>
form {
border-top: 1px dashed #ccc;
width: 375px;
}
label {
position: relative;
width: 200px;
top: 6px;
}
.required label {
font-weight: bold;
}
.field {
border-bottom: 1px dashed #ccc;
padding: 10px 5px 15px 5px;
position: relative;
}
.field input, .field select, .field textarea {
margin: -20px 0 0 175px;
width: 180px;
}
.field textarea {
margin-top: -15px;
}
.field input[type='checkbox'] {
position: relative;
margin-left: 32px;
top: 4px;
}
.field fieldset input {
margin: 0;
width: 20px;
}
.error {
background: #ffcccc;
}
.error_msg {
color: red;
margin: -15px 0 0 0;
padding: 5px 0;
}
input[type='submit'] {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Example Form</h1>
<form action="" method="GET">
{form}
<input type="submit" />
</form>
</body>
</html>
Error in page
ReferenceError: /home/manigandan/workspace/CreatingForm/page.jade:1
> 1| h1= title
2|
3| - if (typeof form !== 'undefined')
4| form(method='post')
title is not defined
at eval at <anonymous> (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:176:8)
at /home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:181:12
at Object.render (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:216:14)
at View.engine (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:243:13)
at View.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/view.js:75:8)
at Function.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/application.js:504:10)
at ServerResponse.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/response.js:753:7)
at /home/manigandan/workspace/CreatingForm/app.js:34:9
at callbacks (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/router/index.js:161:37)
at param (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/router/index.js:135:11)
解决方案 This isn't related to forms
, but to how you pass parameters to render
:
app.get('/', function (req, res) {
res.render('page', {
title: 'Filling out the form...',
form: reg_form.toHTML()
});
});
Notice how the extra locals
is removed.
这篇关于如何在Node.js中使用表单模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文