user1434301 user1434301 - 1 year ago 85
Node.js Question

send large json object as response is takes long time in express

I am using: node, express, routing-controllers

when i try to response large json object, it takes more than 60s.
there is my code :

getAll() {
return db.get('phones')

if i response the same json file , it takes only a few second.
there is my code :

getAll(@Req() request: any, @Res() response: any) {
return new Promise( (resolve, reject) => {
return (response.sendFile(path.resolve(__dirname, "../../db/phones.json")));

How can i solve it in a better way?

(I don't wont to use in lazy loading)

Answer Source

If the same data takes a few seconds when not queried from the DB, it is safe to assume that the issue lies with your db query.

Most db issues can be solved by proper indexing. Try running your query independently, see how long it takes, then add relevant indexes and the speed will increase dramatically. In your case, keep in mind that your query needs to pull 2.3Mb of data OUT of the db, before sending those 2.3Mb to the client. In all likelihood, you're better off with a file anyway.

If you're sure that the db isn't the issue, get rid of the promise in your code, and try : response.send(db.get('phones'))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download