2

I don't understand why req.body is undefined. It always accesses the function in the node.js server '/formstuff' but the req.body is undefined.

When I look at the results it did post to the cmd I don't see my query anywhere. in fact res.query and res.params are empty.

Any help would be appreciated, thanks.

Express:

var express = require('express'),
app = express();
var fs = require('fs'); 
var Promise = require('promise');



// Handle Get Request
app.get('/', function(req, res){
// get stuff from request
var index;

fs.readFile('./form.html', function (err, data) {
if (err) {
    throw err;
}
    index = data;

    res.setHeader("Content-Type", "text/html");
    res.send(index);

});

});


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

    res.send();
});

HTML:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <style type = "text/css">

body
{
    margin:0;
}
#head
{   
    width:100%;
    color:#FAFAFA;
    height:170px;
    text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
    background: #39d800; /* Old browsers */
    background: -moz-linear-gradient(top,  #39d800 0%, #00a008 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#39d800), color-stop(100%,#00a008)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  #39d800 0%,#00a008 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  #39d800 0%,#00a008 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  #39d800 0%,#00a008 100%); /* IE10+ */
    background: linear-gradient(to bottom,  #39d800 0%,#00a008 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#39d800', endColorstr='#00a008',GradientType=0 ); /* IE6-9 */
    font-family: Brush Script MT;
    font-weight:bold;
    font-size:8.5em;
    text-align:center;
}

form div
{
    padding:0.8%;
}
fieldset
{
    font-size: 2em;
    position:relative;
}
input
{
    width: 150px;
    height: 50px;
    font-size: 1.2em;
    clear:both;
}
 </style>
</head>
<body>

<div id = "head">Notifye</div><br>

<br>

 <fieldset>
    <legend>Search</legend>

        <form name = "form" method = "post" action = "http://127.0.0.1:3000/formstuff">

        <input type="text" required = "required" name = "tag" placeholder = "lolcats">
        <div><input type = "submit" name = "submit" value = "Submit" id = "sub"></div>

    </form>

 </fieldset>

</body>
Evan Nudd
  • 21
  • 2

2 Answers2

2

You're missing body parsing middleware.

If you're not uploading files, you can just npm install body-parser and then add app.use(require('body-parser').urlencoded()); before any of your routes.

mscdex
  • 93,083
  • 13
  • 170
  • 135
  • After installing it it throws error saying it cannot find module 'body-parser'. I checked and it does exist in the node modules folder after i installed it. – Evan Nudd Dec 01 '14 at 21:46
  • Works for me on various versions of Express (e.g. 3.x on up). Make sure you're following the examples found here: https://github.com/expressjs/body-parser – martenc Dec 01 '14 at 22:04
0

You need some more packages. I suggest:

var connect    =require('connect'); //
var multer     =require('multer'); // This is used when multipart/form-data are required
app.use(connect.json()); // for json
app.use(connect.urlencoded()); // for application/x-www-form-urlencoded
app.use(multer({ dest: './uploads/'}));// for multipart/form-data 

This is preferable to body-parser which is not safe as explained here. See connect docs and multer docs

k88074
  • 1,742
  • 2
  • 24
  • 39
  • The link given as the reason not to use `body-parser` is not valid anymore. That link describes the Express **3** `bodyParser` middleware that was bundled with Express that *did both* urlencoded *and* multipart. The separate `body-parser` module for Express **4** does not do multipart, only urlencoded and JSON. – mscdex Dec 01 '14 at 23:23