How to create a application level cache object in nodejs - node.js

I am using NodeJS by express framework, now I want to use the memory cache to save objects(almost 3000).
I have thought create a file like
cache.js:
var cache={};
module.exports=cache;
Then in any module I need the cache I can require it:
require('cache')
cache.xx=xxx
However it seems that I can not make sure that the cache object will be create and will be only one copy during the app running. Since the require may use the module cache or not.(from this link:https://stackoverflow.com/a/9210901/306719)
Any suggestion?

That will work just fine. Node caches the module the first time it's loaded, so any additional requires will get the same exported object.

Related

Safe way to load third party module in NodeJs application

I am quite new in NodeJs and here is my issue:
I am developing a small app using Express.
In this app, I want to open a door for third party to install a kind of plugin. The idea is I have a "plugin" folder and they can create a plugin with a bootstrap file like "bootstrap.js"
From this file, they can define route...etc.
The main application will crawl the plugin folder and load the plugin automatically by requiring "bootstrap.js" file.
The problem is from the "bootstrap.js" file they can access everything like the app, request, route object by requiring Node module and modify them.
I want to prevent this from happing. I don't want the plugin to have access to main objects (Express app, request, route. ...) because it could make the application a mess.
Is there any way in Node when I can load a js file and prevent that file to execute something not allowed?
Thank you so much for your help

how to access a different module in multi target application

I'm new to cloud foundry, so I'm not sure, if my thoughts and plans are right. Maybe someone can explain or discuss it with me.
What I want to do:
Implement a MTA (Multitarget Application) with a a html5-module as frontend and a nodeJS-module as backend. Furthermore there should be a mongodb instance, which will be accessed from the nodejs-module. Later it should also get multitenant.
What I already did:
I implemented a simple nodejs-app and connected it to the db. Persisting and calling data with rest works already fine. I implemented a simple sapui5 app, which consumes data from the db with ajax. For now, the node startscript is in the html5 module, so it works somehow. But now I want to separate the modules.
So I created a mta-project with the two modules in webide and imported the two apps.
What I expect to do for it:
For now, I have an approuter, which is in my nodejs-module, but I can not access the webapp folder in the html5-module from here: file not found error: /home/vcap/app//. Is there a possibility to access the webapp-folder in another module over the path "/home/vcap/app/"? Or can I lookup the app-directory anywhere?
I have read, that an approuter-module (nodejs) can be needed, but I don't know exactly what it does. I think it serves the index.html file when opening the url of the whole app?

Cannot access process object in react and non-node js backend (apache) server application

I'm working on a react tutorial, however we are not using node js as the backend server. We are however using babel and webpack and react, but apache with a custom php backend.
Because of this, as far as i have researched, we do not have access to the process object because its a node internal object?
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
This seems like a crucial object that is neccessary for react development but like i said im just a novice so i dont know the answer.
How do we gain access to this object or if not whats our backup plan as i've seen this process object in other react code ive looked at and i want to make sure its available before we start getting heavy into development
The process object is something specific to Node but its usage is widely used with Webpack to provide environment variable. By default Webpack creates a polyfill for the process object.
To create your own variable you can use the Webpak plugin EnvironmentPlugin.

Should I restart node to update cached views when content from external api changes

I have a node app running my blog and my content is coming from an outside api. I use express and currently with dust templates and by default views are cached. However apparently there is no way to clear the cached files without restarting node.
Is it normal that when I update a blogpost I restart node or am I doing something wrong?
I have a webhook, so I can act on the content change, I just don't know what I would correctly do.
Unfortunately you will need to restart the server but modify your express configurations to disable the view caching in the future.
simply you can add something like that:
var app = express();
app.disable('view cache'); //app: is express
if your blog post are stored inside a database, express will only cache you html template but will populate your views with the information.
if your blogposts are new ht;l page each time, I think you will need to restart your nodejs server. But in your express configuration (template configuration I think) you have an option to deactivate the cache.
And/Or you can use pm2 or nodemon to restart your nodejs on every changes (not really the best way)
PS: oups talk to much about different way, forget you have the content from an API. So I think you can remove your cache on dust. The only time I restart nodejs is when I update the javascript/nodejs code.

How can I perform HTTP request caching in a node.js CLI application that persists once the application has exited?

I'm writing a node.js CLI application that uses the GitHub API. Instead of making several HTTP requests to the GitHub every time the app is invoked, I'd like to cache responses and use the ETag and Last-Modified headers to determine if I need to make a request.
How can I persist these requests on a user's machine without resorting to reading from and writing to flat files in the user's home directory?
If you're expecting it to persist when the app isn't running, then storing to a file is your simplest option. You could do it with a redis cache or local sql database, but then your user has to set that up too.
You could use something like memory-cache to have it cache the response while the app is running though, and just rely on a single new HTTP request each time the app starts up.
Node.js has a variable called __dirname which points to the directory the current running script resides in. If a package is installed via NPM, this corresponds to the global node_modules folder, which scripts have read/write access to. I ended up using the trivialdb package to store a JSON file in this directory.
One caveat: the folder will be overwritten if the user updates the package, so if you want to store data that persists through multiple installs of your package, storing in the user's home directory is probably the best bet.

Resources