可以在expressjs中做两条发布路线吗? [英] is it possible to do two post routes in expressjs?

查看:38
本文介绍了可以在expressjs中做两条发布路线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Expressjs应用程序.它包含公共,视图,路线文件夹和app.js文件.

I created a Expressjs Application. It contains public, views, routes folders and app.js file.

我在app.js中有一条用于"/"路径的后置路由器,

I am having a post router for "/" path like this in app.js,

app.post('/', store.home_post_handler);

和玉器密码

#display
    #login        
        form(method='post')
            | Enter your name if you want to be a ninja
            div
                input(type='text', name='username')
                input(type='submit', value='Log In')

我的问题是,是否可以在一个页面中使用两个post方法?

My question is, is it possible to have a two post methods in a single page?

推荐答案

如果您希望客户端页面上的两个表单执行不同的操作,则区分表单的最简单方法是通过更改表单元素的 action 属性.

If you want two forms on a client page, that do different things, the simplest way to differentiate the forms is to have them POST to different URL's by changing the action attribute of the form element.

如果您需要将表单POST到相同的URL,则应使用@hexacyanide的解决方案.

If you require the forms to POST to the same URL, you should use @hexacyanide 's solution.

// app.js
app.get('/form', function(req, res){
  res.render('formTemplate');
});

app.post('/form1', function(req, res){
  console.log(req.body.foo);
});

app.post('/form2', function(req, res){
  console.log(req.body.bar);
});

// formTemplate.jade
!!!
  body
    form(action='form1', method='post')
      label(for='foo') Foo:
      input(type='text', name='foo', id='foo')
      input(type='submit', value='Submit Foo')
    form(action='form2', method='post')
      label(for='bar') Bar:
      input(type='text', name='bar', id='bar')
      input(type='submit', value='Submit Bar')

这篇关于可以在expressjs中做两条发布路线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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