0

I have a client side angular app that sends POST request to node js server.

order(market: string, price: string, side: string, size: string): Observable<any> {
        var payload = { market: market, order: { price: price, side: side, size: size } };
        return this.http.post<any>('http://localhost:3000' + "/orders", JSON.stringify(payload))
            .pipe(
                tap((res: any) => {
                   console.log(res);
                }),
                retry(3), // retry a failed request up to 3 times
                catchError(this.handleError) // then handle the error
            );
    }

nodejs with express-generator:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();

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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var corsOptions = {
    origin: 'http://localhost:4200',
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 
  }

app.use(cors(corsOptions))

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.post('/orders', (req, res) => {
    console.log(req.body)   
});

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;

angular app can reach to a method in nodejs app

app.post('/orders', (req, res) => {
    console.log(req.body)
});

but the req.body is undefined. Am I doing something wrong here?

bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • Did you check that thread? https://stackoverflow.com/questions/9177049/express-js-req-body-undefined – David May 10 '20 at 10:22

2 Answers2

1

Your back-end code looks good to me. I believe that the problem is caused by the front-end code. Specifically by this line:

this.http.post<any>('http://localhost:3000' + "/orders", JSON.stringify(payload))

Do not stringify the data that you're sending it to the back-end. Change it like so:

this.http.post<any>('http://localhost:3000' + "/orders", payload)
ionut-t
  • 1,121
  • 1
  • 7
  • 14
0

You need to use body-parser, which is ExpressJS middleware that lets you parse your POST requests. This is a fairly well documented problem, you can also find a similar S/O question here

Raghav
  • 470
  • 3
  • 13