简体   繁体   中英

how to submit values of html form from one page to another using node.js express framework

am using node.js express framework and m totally new in this environmentnd m stuck in a problem which is i have to submit my html form values to the next page using node.i have three folder there one controller 2nd views and third is public.. here is my html form

<form action="/myaction" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" placeholder="Enter your full name" />
            <label for="email">Email:</label>
            <input type="email" id="email" placeholder="Enter your email address" />
            <label for="message">Message:</label>
            <textarea id="message" placeholder="What's on your mind?"></textarea>
            <input type="submit" value="Send message" />
        </fieldset>
    </form>

and here is my app.js

var express = require('express');
var http = require('http');
var cons=require('consolidate');
var path = require('path');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var Controllerdefault = require('./controllers/default');
var app = express();
app.engine('html',cons.swig);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
var hour = 3600000;
var day = hour*24;
var week = day * 7;
app.use(express.static(path.join(__dirname,'public'),
{
maxAge:week}));
app.use(express.bodyParser());
app.post('/myaction', function(req, res) {
  res.send('You sent the name "' + req.body.name + '".');
});
app.get('/' , Controllerdefault.index );
app.get('/home' , Controllerdefault.index2);
app.get('/new' , Controllerdefault.index3);
console.log('Express app started on port 3000');
app.listen(3000);

any body tell me how can i submit values because i do it but error come in cmd is like that..

Error: Most middleware (like bodyParser) is no longer bundled with Express and m
ust be installed separately. Please see https://github.com/senchalabs/connect#mi
ddleware.
    at Function.Object.defineProperty.get (C:\Users\Tahir\Desktop\node_modules\e
xpress\lib\express.js:89:13)
    at Object.<anonymous> (C:\Users\Tahir\Desktop\firstapp\app.js:20:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

You should install body-parser separately, because you are using Express 4. npm install body-parser and then use it

                   var bodyParser = require('body-parser');

for more details see https://github.com/expressjs/body-parser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM