I am currently working on a MEAN stack project (Angular 5 + NodeJS), and I am relatively new to NodeJS. Currently I am facing an issue when it is deployed to production server which is hosted in Amazon EC2 servers which is in Linux. The server is getting crashed with following error.
Error: EMFILE: too many open files, open '/var/app/current/dist/index.html'
I have searched for solutions for this problem but unfortunately it didn't help. One of the solution was to increase the limit of open files and I did it, but today the server got crashed again. The most surprising fact is that I don't have any file operations in my application. Is this a code issue (like memory leak) or a OS limitation?. One more thing, during the development phase I was hosting my application in Heroku, over there I didn't get this issue. How can I permanently fix this issue? Please help me with some suggestions on how to identify the issue and fix this.
Here is my server.js code (removed api sections)
// API file for interacting with MongoDB
const config = require('./server/config');
require('./server/mongodb/collections');
require('./server/mongodb/initialisers');
// Parsers
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));
app.use(session({
secret: 'mysecret',
resave: true,
saveUninitialized: true,
}));
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });
// Send all other requests to the Angular app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
//Set Port
const port = process.env.PORT || '3000';
app.set('port', port);
/*app.listen(port, '0.0.0.0', function() {
console.log('Listening to port: ' + port);
});*/
const server = http.createServer(app);
server.listen(port, () => console.warn(`Running on localhost:${port}`));
UPDATE: 29/08/2018
I have checked the logs from the server, I got these errors
Error: EMFILE: too many open files, open '/var/app/current/dist/styles.92f52039f26f58ffb65b.bundle.css'
Error: EMFILE: too many open files, open '/var/app/current/dist/index.html'
The css files are not directly called by my code, can anyone tell me why this issue is happening with a css files and any suggestions that I can try out for resolving this issue?
Thanks, Kapil