1

I am new to nodejs. I have one API which returns file in response. Currently when I get the request, I read the file every time and send it. Can we do caching in memory to speed up?

I checked this package. can we use this package to speed up. https://www.npmjs.com/package/memoizee

 server.get('/user', (req, res) => {
    res.sendFile(path.join(distFolder,'user','index.html'))
  });

Current what I am doing

Right now I am running node server.when /user request come on I am sending html file .while is working fine.

can we optimise this ? can we cache this file ?

Drashti Kheni
  • 1,065
  • 9
  • 23
  • you're sending a static file - what would you cache on the server? – Jaromanda X Aug 25 '22 at 03:53
  • You should look into the [HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) if you just send to a static file back. – Pylon Aug 25 '22 at 03:55
  • @JaromandaX static file content –  Aug 25 '22 at 03:57
  • @JaromandaX can we set some header so that it file come from cache second request or other than first request –  Aug 25 '22 at 03:59
  • Oh, so you want to cache the file in memory so you don't have to read the file every time you send it - if on the other hand you're talking about client (browser) caching so the client doesn't need the response every time ... there is [this](http://expressjs.com/en/resources/middleware/serve-static.html) (I'm assuming you're using expressjs of course) – Jaromanda X Aug 25 '22 at 04:02
  • @JaromandaX..I am taking about this <>> client will always send request but I need to send from cache –  Aug 25 '22 at 04:09
  • memoizing seems to be what you want then – Jaromanda X Aug 25 '22 at 04:17
  • can you please help me how we memoizing in node js . first request with from sendfile , rest req will be from memoizing –  Aug 25 '22 at 04:21

1 Answers1

0

A simple approach

import { readFileSync } from "node:fs";

const cache = {};
const simpleCache = path => {
    if (!cache[path]) {
        cache[path] = readFileSync(path).toString();
    }
    return cache[path];
};
server.get('/user', (req, res) => {
    res.send(simpleCache(path.join(distFolder,'user','index.html')));
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • sorry I understand the code .. but using above code it downloading file ..it is not showing anything on browser... but when I was using this code ` res.sendFile(path.join(distFolder,'user','index.html')) `. it is showing HTML content –  Aug 25 '22 at 04:43
  • I must've done something wrong then - probably the format of the data `readFileSync` gets is wrong ... perhaps you need `fs.readFileSync(path).toString()` – Jaromanda X Aug 25 '22 at 04:48
  • you did remember to import/require `fs`? right? – Jaromanda X Aug 25 '22 at 05:00
  • sorry still it downloading..not working I will try another solution –  Aug 25 '22 at 05:09
  • @naveen - that's odd, works for me like a bought one ... and ... you get no errors at all? – Jaromanda X Aug 25 '22 at 05:20
  • I am not getting error but if I am hitting `localhost:3000/user` --> it download the file with name `user`. content is there in file –  Aug 25 '22 at 05:25
  • so, it's getting a file, just not the right one? – Jaromanda X Aug 25 '22 at 05:37