How to get data passed from a form in Express (Node.js)

前端 未结 3 1675
挽巷
挽巷 2020-12-04 20:59

I would like to get data that are passed from a page using a form and use that data in the page that is redirected.

I have this form in my client side:



        
3条回答
  •  情书的邮戳
    2020-12-04 21:42

    IMPORTANT: This is deprecated, the accepted answer is the correct solution.


    To allow express to handle form data nicely you need to ensure you have bodyParser included like so:

    var express = require('express'),
        app = express.createServer();
    
    app.use(express.bodyParser());
    //the rest of your configuration
    

    Then in your POST handler you can access the form body through the Request.body property like so:

    app.post('/game', function (req, res) {
        res.render('some-file', { name: req.body.name });
    });
    

    Also you'll need to use a templating engine (such as Jade) if you're intending to output the form data in the response.

提交回复
热议问题