I wrote node.js small app for myself which was using small js code to clear require cache before handling request:
let clearCache
if (process.env.NODE_ENV === 'development') {
const {cache} = require
clearCache = (except) => {
for (let key in cache)
if (!except.includes(key) && key.indexOf('/node_modules/') === -1)
delete cache[key]
}
} else {
clearCache = () => {}
}
const { createServer } = require('http')
const persistentPaths = ['/path/to/my/db/file']
createServer((req, res) => {
clearCache(persistentPaths)
require('./handleRequest')(req, res)
}).listen(3000, () => {})
I believe this is the most efficient way to working with app in development.
When I change code it applies immediately and this worked fine to me.
Now I would like to work with express.js, and it seems to impossible to use this approach.
Okay, seems like it is self-answering question and I need to use nodemon.
But I really don’t want to use tool which will be watch all files and will relaunch whole server. I’m afraid that it would be much slower.
In my example you can see that I reload files except db file, so my app in development connects to database only once and keeps that connection. In case of using nodemon app need to load also all node_modules code, make new connection to db each time when code changed, maybe it will connect each time to postgres and to redis.
Question is: I can’t be the one who doing it this way, are there solutions to reload modules for express.js?
2
Answers
Did it! Would be glad if you will share your thoughts on it.
Because I can google only nodemon and chokidar solutions, maybe I'm doing something wierd, here is my solution:
server.ts:
And routes.ts:
Usage: NODE_ENV=development NODE_PATH=src node src/server.js
(my IDE compiles ts to js while editing and put it right there, if you are using VS code it might be more complicated, I guess)
You don’t need to write your one util. You can use module like chokidar for so. You can specify directory and ignore the same time.
You can reload on callback the watch.
Sample from there wiki:: https://github.com/paulmillr/chokidar