2

I'm using Node as a proxy server between a React App and Microsoft Dynamics Nav. I make CRUD requests in both OData and SOAP web services. When Dynamics Nav Server responds with an error, I need to show that error in React App. The problem is that I always get a http 200 status code in Node.js, even when I get an error on server (i.e. 404). This is an example response of a put request, in both Node.js and Postman:

Postman

enter image description here

Node.js

enter image description here

This is server.js file:

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

const server = app.listen(5000, function() {
    console.log(new Date().toISOString() + ": server started on port 5000");
});

server.on('connection', (socket) => {
    console.log('New connection...');
});


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

// parse application/json
app.use(bodyParser.json());
app.use(cors());

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

app.get('/', function (req, res) {
    res.send('Express for courier is working on IISNode!');
});

app.use(require('./routes/routesLogin'));
app.use(require('./routes/routesCustomers'));
app.use(require('./routes/routesContacts'));
app.use(require('./routes/routesPostCodes'));
app.use(require('./routes/routesCountriesRegions'));
app.use(require('./routes/routesDepots'));
app.use(require('./routes/routesPUOrder'));

This is the function for a put request:

    ModifyPUOrder: function (req, res) {
        console.log('MODIFY');
        const PUOrderNo = req.body.puOrderNo;
        const updatedValue = req.body.updatedValue;
        console.log('MODIFY PU', PUOrderNo);
        console.log('UPDATEDVALUE', updatedValue)
        const url = `${base}/PUOrderCard('${PUOrderNo}')`;

        request =   `{ ${updatedValue} }`;
        
        put(username, password, url, request).then(response => {
            console.log(response);
            res.send(response);
        })
    },

Any help would be much appreciated!

1 Answer 1

3

The default status code for a response in Express is 200, you can change this before you call send

res.status(400).send('Bad request')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.