0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

i did it same but the same error occour as i mentioned
Your problem is here: app.use(express.bodyParser()); try app.use(bodyParser()); or app.use(bodyParser.json());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.