javascriptnode.jsjsonexpressoracle-commerce

I get error when sending data using express from node server to client side JavaScript


I am making a post call from ajax to node server. Server makes the call and gives the response. But also below error displays in the cmd prompt

express deprecated res.send(status, body): Use res.status(status).send(body) instead server.js:203:26
_http_server.js:248
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
^RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: exporting started
at ServerResponse.writeHead (_http_server.js:248:11)
at ServerResponse._implicitHeader (_http_server.js:239:8)
at write_ (_http_outgoing.js:650:9)
at ServerResponse.end (_http_outgoing.js:761:5)
at ServerResponse.send (D:\Impex-Node server\node_modules\express\lib\response.js:221:10)
at ServerResponse.json (D:\Impex-Node server\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (D:\Impex-Node server\node_modules\express\lib\response.js:158:21)
at Request._callback (D:\Impex-Node server\server\server.js:203:26)
at Request.self.callback (D:\Impex-Node server\node_modules\request\request.js:185:22)
at Request.emit (events.js:310:20) {
code: 'ERR_HTTP_INVALID_STATUS_CODE'
 }
[nodemon] app crashed - waiting for file changes before starting...

What's wrong here? Can someone help me to know this

Below is the request call:-

     request.post(url, {
        body :req_body,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${access_token}`
        }

    }, (error, res, body) => {
        if (error) {
            console.log('An error occured while loading the page', error)
            return
        }

        let data = JSON.parse(body);
        console.log("export data", data);
        console.log("export call:", data)
        response.send("exporting started", data)

        }

Solution

  • Read the first line of the error message:

    express deprecated res.send(status, body): Use res.status(status).send(body) instead

    Now look at where you call send:

    response.send("exporting started", data)
    

    You are passing two arguments to send, but the documentation shows it only accepts one argument.

    There was a time when it accepted two arguments, but even then the first argument needed to be an HTTP status code, which the string "exporting started" isn't.