Simple error handling in node js with express?

halfiranian

Ok you can probably tell I'm a bit new to this Node js lark but I've been stumped on this for a while and I have searched high and low for an answer I can understand.

I created a simple json serving Twitter server for some tests (which I call with /tweets.json?search=SOMETHING and works fine), but I don't know how to stop this app crashing when Twitter responds with an error.

Obviously if the query string is not in the url or there's any other issue from the Twitter API there'll be an error, but how can I stop this incredibly simple app from crashing every time that happens?

I know this is probably very basic so big apologies in advance.

var Twit = require('twit');
var express = require("express");

var app = express();

var T = new Twit({ XXXXX KEYS XXXXXX });

app.get("/tweets.json", function(req, res) {

    T.get('search/tweets', { q: req.query.search, count: 100, result_type: 'popular' }, function(err, reply) {
        if(err){
            res.jsonp(err);
        }
        res.jsonp(reply);
    })
});

app.listen(process.env.PORT || 3000);

console.log("listening on port 3000");
qubyte

You have a double callback. This bit:

if (err) {
    res.jsonp(err);
}

res.jsonp(reply);

calls res.jsonp both times when there is an error. To correct that part you need to put a return in the branch:

if (err) {
    return res.jsonp(err);
}

res.jsonp(reply);

res.jsonp is the wrong method for returning an error too. Perhaps something like:

if (err) {
    return res.send(500, err.message); // Status code 500 with a message.
}

res.jsonp(reply);

Would be better.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add error handling to node / express mySQL functions

Handling cancelled request with Express/Node.js and Angular

Simple API Calls with Node.js and Express

error handling in asynchronous node.js calls

Error handling with node.js streams

Error handling principles for Node.js + Express.js applications?

Simple get request with node.js and express

Node.js MySQL Error Handling

Node.js (Express) error handling middleware with router

Node JS Express Handling multiple requests

'missing routes' error handling in node.js express app (failing)

Customized error handling in Express.js

Error in graphql in Express , node js

Node.js - Package error handling

Express js installation error in node

Simple Express.js application running with error

Handling <select> form parameter in node.js/express

simple node.js server not error handling failed request

Express js - Error handling in middleware

Handling 503 Error in Express/Node.js especially in TryCatch case

Error handling unvalid data with: invalid + express node.js

Node.js mysql error handling in promise

Node Express RESTful API default engine for error handling

Node js Error Handling it crash the server

Synchronous error handling in node.js router

Using Node.js / Express is it possible to next() an error from inside an IIFE and advance to the error handling middleware?

Handling express error in Next.js with axios

Express JS query Error Handling not working

Appsody :: Run simple node js express application gives error ( MacBook Pro - M1 -2020 )