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:
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.
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 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.
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.
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).