Very simple test script for handling POST requests with NodeJS: get getting 'undefined'

Hello, I’m trying to test a very simple script (similar to a “hello world” example) with NodeJS to handle POST requests, but I keed getting “undefined” for whatever I try.

I have already installed the body-parser.

This is the script:

var express = require('express');
var http = require('http');
var app = express();
var bodyParser  = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.set('port', process.env.PORT || 3000);
http.createServer(app).listen(app.get('port'), function(){  
	console.log('Express server listening on port ' + app.get('port'));
});
app.post('/users', function(request, response) {    
	var username = request.body.username;
	response.send('Hello, ' + username + '!');
});

And I’m using Postman to send the request. This is the request I’m sending:

http://localhost:3000/users?POST /users HTTP/1.1=&Host= localhost:3000&Authorization= ApiKey &appClient=xxxxxxxxxxxxxxxxxxxxxxxxxx&Cache-Control= no-cache&----WebKitFormBoundaryE19zNvXGzXaLvS5C=&Content-Disposition= form-data; name=“username”&oscar1234----=&WebKitFormBoundaryE19zNvXGzXaLvS5C=

The problem is at the line:

var username = request.body.username;

It does not have any ‘username’, but if it was working fine it should be ‘oscar1234’

What I’m doing wrong?

TIA.

And why do you think this? When you are using request.body.username you are asking for the username parameter of the body. Try putting in your request “username=oscar1234” as one of the parameters. See if it works then.

There’s nothing wrong with the script, rather as Martyr2 says, you are missing the correct parameter from your request.

curl -X POST -d 'username=oscar1234' http://localhost:3000/users
Hello, oscar1234!

I also updated what you posted to use a more modern syntax. You might want to look at ES6(+) and the changes it brought to the language.

const express = require('express');
const http = require('http');

const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.set('port', process.env.PORT || 3000);

http.createServer(app).listen(app.get('port'), () => {
  console.log(`Express server listening on port ${app.get('port')}`);
});

app.post('/users', (request, response) => {
  const { username } = request.body;
  response.send(`Hello, ${username}!`);
});

Well, I’m taking an online course of NodeJS, that it seems to be a bit outdated, but still I’m trying to make the things work as taught in the course.

This was the original code for the script:

var express = require('express');
var http = require('http');
var app = express();
app.use(express.bodyParser());
app.set('port', process.env.PORT || 3000);

http.createServer(app).listen(app.get('port'), function(){  
  console.log('Express server listening on port ' + app.get('port'));
});

app.post('/users', function(request, response) {    
	var username = request.body.username;    
	response.send('Hello, ' + username + '!');
});

I changed some things (as posted in the OP) because the original code didn’t work with the current NodeJS version.

And the sample POST request they have is:

POST /users HTTP/1.1
Host: localhost:3000
Authorization: ApiKey 
appClient:xxxxxxxxxxxxxxxxxxxxxxxxxx
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="username"
oscar1234----
WebKitFormBoundaryE19zNvXGzXaLvS5C

Is that wrong?
I would like to understand the issue, not just to make it work without understanding.

Thank you very much.

PS: and yes, I saw that now is available other syntax that has ‘=> {’, but I still not tried it because I focused in making work this simple script, although with the old syntax.

I tried that from a new command prompt and I get:

Hello, undefined!

I’m using Windows 10.

PS: and with the new syntax that you posted I get the same.
Perhaps I need to reinstall NodeJS or something, IDK.

Hi,

I’m on the hop (about to go out), so excuse the short reply.

Try to make the post request like this:

This is using the code you supplied in post #1.

1 Like

OK, that worked. Thank you very much.

Still wondering why the curl attempt didn’t work.

Can you try this (uses double quotes instead of single):

curl -X POST -d "username=oscar1234" http://localhost:3000/users

Does that make a difference?

1 Like

Yep, that worked!

Thank you!

Cool. Seems to be this issue:

1 Like

Out of interest, could you share a link to the tutorial you are following?

It is in Spanish: https://conectaempleo-formacion.fundaciontelefonica.com/web/ar-programacion-con-javascript-ftm-ed-4/

You need to register to see it and be able to take it.

Ah, shame.

I would caution against following a tutorial that is too out of date, though. The JS landscape moves very fast and you’ll end up just creating more problems than it’s worth.

That said, good luck :slight_smile:

I’m taking it as part of the formation, because I suppose I’ll find outdated code and perhaps at a point I might have to work with old code in some existing project.
But the main reason that I want to finish it (that I’m already almost at the end of the course) is because they issue a certificate (not sure if I’ll need it but better to have it).
I know this is only the start, I’ll have to study a lot more.
And I realized just recently what you say, that it changes very quickly.

1 Like

Yeah, didn’t want to knock the course or your efforts. Rather just something to be aware of.

Yes, thank you.
I’m very new (but not new to programming), it is only one month that I’ve been studying JavaScript and I’m seeing the big picture: several frameworks with different syntax’s and also changing very fast.
So far I saw JQuery, AngularJS and now NodeJS. This course finishes there but I have in mind to get also familiar with React. And then may be to try to do something (some small project to get some practice).