Randomblue
Randomblue

Reputation: 116403

expressJS request object

I'm trying to go through the expressJS tutorial. This is my server code:

var express = require('express');
var app = require('express').createServer();

app.use(express.bodyParser());

app.post('/', function(request, response) {
    console.log('Inside the post request!');
    console.log(request);
    response.send(resquest.body);
});

app.listen(3000);

and here is the POST request I am simulating:

$.ajax({
    url: 'http://localhost:3000',
    type: 'POST',
    datatype: 'json',
    data: {hello: 1},
    success: function () {
        console.log('Success!');
    },
    error: function () {
        console.log('Error!');
    }
});

The problem is that the request object does not seem to contain data: {hello: 1}. Instead it is a big mess of under-the-hood parameters. Am I doing something stupid?

Upvotes: 1

Views: 4446

Answers (3)

Kato
Kato

Reputation: 40582

You've written resquest.body instead of request.body; when you fix that, you'll be able to use request.body.data as others have pointed out.

Upvotes: 2

Sean Hill
Sean Hill

Reputation: 15056

You need to look at request.body. When you do request.body, you get {hello: 1}.

Upvotes: 1

JP Richardson
JP Richardson

Reputation: 39435

I think that since you're not setting the content type to multipart/form-data it's assuming form encoded data. In which case, you'd set your data in your ajax request to be:

data: 'hello=1'

Set your content-type to: application/x-www-form-urlencoded

Access it via request.body.hello. It's been awhile, but try that.

Upvotes: 3

Related Questions