Recommand · May 23, 2021 0

Nodejs with express not listening/catching webhooks

I’m trying to catch a json webhooks using Nodejs and Express the code runs fine and i can run npm start but it doesn’t work.
This is the code im running on the webserver. Any idea on where im making mistakes?

Thanks

var express = require('express');
app = express()
port = 3000;

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

app.post('/', function (req, res) {

    var prod = req.body
    console.log(req.body)

    if (prod.total > 0){
    for (var i = 0; i < prod.total; i++ ){
        var sku = body.productos[i].codigo_var;
        var stock = body.productos[i].stock;
        var precio = body.productos[i].precios[0].precio;

        var actualizar = {
            sku,
            stock,
            precio
            }
        console.log(actualizar)
        }

var server = app.listen(port, function () {

    var host = server.address().address
    var port = server.address().port

    console.log('App en http://%s:%s', host, port)

});

It looks like you are missing some closing brackets for the POST route.

app.post('/', function (req, res) {
  var prod = req.body
  console.log(req.body)

  if (prod.total > 0) {
    for (var i = 0; i < prod.total; i++ ) {
      var sku = body.productos[i].codigo_var;
      var stock = body.productos[i].stock;
      var precio = body.productos[i].precios[0].precio;

      var actualizar = {
        sku,
        stock,
        precio
      }
      console.log(actualizar)
    }
  // Missing brackets
  }
});

var server = app.listen(port, function () {
  var host = server.address().address
  var port = server.address().port

  console.log('App en http://%s:%s', host, port)
});