-2

I am learning how to build a web app using express, node, and angular. I have a post request from angular, and I can successfully send that to the router in login.js:

var user = {username: $scope.userName, password: $scope.password};

$scope.login = function() {
    console.log('attempting to log in,,');
    console.log("The useranem" + $scope.userName + " " + $scope.password + " " + user);
    $http.post('/userLogin', user).then(successCallback, errorCallback);
};

index.js

var express = require('express');
var router = express.Router();
var mysql = require('mysql');

var connection = mysql.createConnection({
    host      : 'localhost',
    user      : 'root',
    password  : 'xxxxx',
    database  : 'xxxxx'
});

connection.connect();

router.post('/userLogin', function(req, res, next){
   console.log('the user name is ' + req.body);
});

module.exports = router;

enter image description here

From the image, I can see that I was able to print out the body of the post request as objects. Is this because I don't have body parser in index.js. I already installed body parser in app.js,

1) do I have to require app.js in index.js to use the body parser?

2)And once I am able to parse the body, how do I access the varaibles in the body. Would it be req.username and req.password?

3) This question is not as related, but in my app.js I have app.use('/', login). How do i determine what the path should be? Should it be the same as the express router such as /userLogin?

This is my app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql = require('mysql');

var routes = require('./routes/index');
var register = require('./routes/register');
var users = require('./routes/users');
var login = require('./routes/login');

var app = express();

//establish database connection
var connection = mysql.createConnection({
  host      : 'localhost',
  user      : 'root',
  password  : 'xxxxx',
  database  : 'xxxxx'
});

connection.connect();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/', login);
//add route for registering account

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

Note: most of the stuff in app.js was created using express generator.

mscdex
  • 93,083
  • 13
  • 170
  • 135
simhuang
  • 305
  • 2
  • 6
  • 17
  • @ShaunScovil I used it in app.js as I mentioned in the question. Do i have to include it in index.js as well? – simhuang Jan 08 '16 at 02:22
  • Possible duplicate of [How to access the request body when POSTing using Node.js and Express?](https://stackoverflow.com/questions/11625519/how-to-access-the-request-body-when-posting-using-node-js-and-express) – Mouneer Feb 06 '18 at 12:39

2 Answers2

0

Looks like you need to access the data from req.body that you want. So if the object you want to access from that is user from the json object you'd need to access req.body.user

httpNick
  • 2,372
  • 1
  • 19
  • 34
0

You are trying to concatenate an object with a string. This forces the standard output to call the method toString() from the object which results in [object Object] if it's not overridden with a custom implementation.

To see the whole object, you can use: console.log(req.body); then you will be able to see the object.

Mouneer
  • 10,467
  • 2
  • 32
  • 43
user1318723123
  • 643
  • 5
  • 19