I am working on my ubuntu, and this always hangs :
router.route('/articles')
// get all the articles (accessed at GET http://localhost:8080/api/articles)
.get(function(req, res) {
Article.find().sort('-created_at').exec(function(err, articles) {
if (err)
res.send(err);
res.json(articles);
});
});
but not just this one, it seems to be the same with all my collections, but I can see them fine in mongo shell or Robomongo.
I don't think it is a connection error because I don't have any :
// database
var mongoose = require('mongoose');
console.log(config.database);
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/test', function(err) {
if (err) {
console.err(err);
} else {
console.log('Connected');
}
});
mongoose.connection.on('error', function(err) {
if (err) {
console.err(err);
} else {
console.log('Connected');
}
});
Dont know if it's relevant but I have warnings when using the mongo shell :
2015-09-08T19:07:12.200+0100 I CONTROL [initandlisten]
2015-09-08T19:07:12.200+0100 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 31125 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
I am very surprised because it worked just a few days ago, I am starting to think I may have a problem with my system.
FYI my full project on github : https://github.com/mael-jarnole/website
I have been struggling with this issue the whole afternoon, and could'nt get anything done.
console
---------$ nodemon server.js
8 Sep 19:26:19 - [nodemon] v1.4.1
8 Sep 19:26:19 - [nodemon] to restart at any time, enter `rs`
8 Sep 19:26:19 - [nodemon] watching: *.*
8 Sep 19:26:19 - [nodemon] starting `node server.js`
mongodb://localhost:27017/test
Listening on: 8080
css file written on disk successfully !
Connected
Something is happening with the API.
Related
I am trying to run my node/express app on AWS EC2 but am getting errors from Redis, it will not connect to the server and I am not sure what the problem is??
Here is the error I get after the command "npm run production"
Is there a special configuration when running Redis remotely vs locally?
Thank you!
ERROR
[ec2-user#ip-000-00-00-00 application-node-app]$ npm run production
> task-manager#1.0.0 production
> env-cmd -f ./config/prod.env nodemon src/index.js
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
server is live on port 3000
Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
127.0.0.1:6379
// 9x Redis retry errors, all identical
REDIS CODE
const mongoose = require(`mongoose`)
const redis = require(`redis`)
const util = require(`util`)
const redisUrl = `redis://127.0.0.1:6379`
const client = redis.createClient(redisUrl)
client.hget = util.promisify(client.hget)
const exec = mongoose.Query.prototype.exec
client.on("error", function (err) {
console.log("Error " + err);
});
mongoose.Query.prototype.cache = function(options = {}) {
this.useCache = true
this.hashKey = JSON.stringify(options.key || `default`)
return this
}
mongoose.Query.prototype.exec = async function () {
if (!this.useCache) {
return exec.apply(this, arguments)
}
const key = JSON.stringify(Object.assign({}, this.getQuery(), {
collection: this.mongooseCollection.name
}))
const cacheValue = await client.hget(this.hashKey, key)
if (cacheValue) {
console.log(`cached DATA!`)
const doc = JSON.parse(cacheValue)
return Array.isArray(doc) ? doc.map(d => new this.model(d)) : new this.model(doc)
}
const result = await exec.apply(this, arguments)
client.hset(this.hashKey, key, JSON.stringify(result), `EX`, 10)
return result
}
module.exports = {
clearHash(hashKey) {
client.del(JSON.stringify(hashKey))
}
}
There isn't any change in redis to run it locally or remotely.
What you need to make sure instead is, Do you have connectivity to redis from your EC2 instance.
Worst case you can try installing redis-cli on to the EC2 instance and figure out from there. I believe it might be port forwarding issue or IP Tables issue.
You should of course restart from a fresh EC2 instance once the testing is done.
Edit: One thing I wish to add here though, Even though I said there is no change in redis, make sure that it's bound on 0.0.0.0 and not on 127.0.0.1 and make sure to check the port config
I've been using Google Cloud Run for a year now and the issue with cloud run containers restarts / new container start is from the beginning.
I've hosted Node + MongoDB app in Cloud Run, but cloud run container is restarting frequently. It's getting around 10 - 12 requests / second, couldn't find any performance bottleneck, requests are serving smoothly, sometimes requests are served more than normal time, might be new container instance cold start delay.
The issue I am facing is the HIGH Number of connections to the MONGODB Server. After some research I could find that I've to close mongodb connection on node process exit so I've added a graceful shutdown function.
// Function to terminate the app gracefully:
const gracefulShutdown = async () => {
console.log(`MONGODB CONNECTION CLOSED!`);
await mongoose.connection.close();
};
// This will handle process.exit():
process.on('exit', gracefulShutdown);
// This will handle kill commands, such as CTRL+C:
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
// This will prevent dirty exit on code-fault crashes:
process.on('uncaughtException', gracefulShutdown);
But even after adding this, I couldn't find this graceful shutdown function is invoked while checking logs.
Does google cloud run really signals when the nodejs process in the container crashed?
Is there any way to identity a container restart or new instance creation in cloud run?
Here is the MongoDB connection code
exports.connect = () => {
try {
mongoose
.connect(MONGO.URI, {
useCreateIndex: true,
keepAlive: 1,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then((docs) => {
console.log(`DB Connected`);
})
.catch((err) => {
console.log(`err`, err);
});
return mongoose.connection;
} catch (err) {
console.log(`#### Error Connecting DB`, err);
console.log(`Mongo URI: `, MONGO.URI);
}
};
Sometimes cloud run issues a high number of connections to MONGODB, and hits the connection limit of 1500 connections.
Any suggestions are appreciated! I've been facing this issue for a year now.
You should not start the node process using npm or yarn but directly as CMD ["node", "index.js"] (when you are inside a Docker container using Dockerfile)
Explanation here https://maximorlov.com/process-signals-inside-docker-containers/
I have connected mongodb using mongoose.
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI, (err) => {
if (!err) { console.log('MongoDB connection succeeded.'); } else { console.log('Error in MongoDB connection : ' + JSON.stringify(err, undefined, 2)); }
});
After rebooted my system I am facing below the issues.
I am not able to connect mongodb. If i start using mongod command i am getting this error:
***aborting after fassert() failure
2020-07-06T15:31:16.058+0530 F - [WTCheckpointThread] Got signal: 22 (SIGABRT).
mongod.exe ...\src\mongo\util\stacktrace_windows.cpp(246) mongo::printStackTrace+0x43
mongod.exe ...\src\mongo\util\signal_handlers_synchronous.cpp(241) mongo::`anonymous namespace'::abruptQuit+0x81
ucrtbase.dll raise+0x1dd
ucrtbase.dll abort+0x31
mongod.exe ...\src\mongo\util\assert_util.cpp(145) mongo::fassertFailedWithLocation+0xd6
Also i am not able to use anything like find,save ect..mongoose query.If i do anything i am getting below error
MongoError: pool destroyed
at Pool.write (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\connection\pool.js:840:8)
at _command (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\wireprotocol\command.js:128:10)
at command (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\wireprotocol\command.js:28:5)
at Object.query (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\wireprotocol\query.js:57:3)
at Server.query (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\topologies\server.js:644:16)
at FindOperation.execute (D:\Projects\Angular\project\server\node_modules\mongodb\lib\operations\find.js:24:12)
at D:\Projects\Angular\project\server\node_modules\mongodb\lib\operations\execute_operation.js:151:17
at Server.selectServer (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\topologies\server.js:832:3)
at Server.selectServer (D:\Projects\Angular\project\server\node_modules\mongodb\lib\topologies\topology_base.js:342:32)
at executeWithServerSelection (D:\Projects\Angular\project\server\node_modules\mongodb\lib\operations\execute_operation.js:137:12)
at executeOperation (D:\Projects\Angular\project\server\node_modules\mongodb\lib\operations\execute_operation.js:75:7)
at Cursor._initializeCursor (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\cursor.js:536:7)
at Cursor._initializeCursor (D:\Projects\Angular\project\server\node_modules\mongodb\lib\cursor.js:185:11)
at nextFunction (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\cursor.js:739:10)
at Cursor._next (D:\Projects\Angular\project\server\node_modules\mongodb\lib\core\cursor.js:202:5)
at fetchDocs (D:\Projects\Angular\project\server\node_modules\mongodb\lib\cursor.js:833:16) {
[Symbol(mongoErrorContextSymbol)]: {}
}
How to resolve the issue?
The following issue consists of mssql, Nodejs, Gulp, Express, and SQL Server Express. I am able to login to SQL Server Express successfully. However, the returned value is undefined when I use the bookRoute.js code snippet without callback. Yet, when I use a callback I get the data. However, I don't understand why.
app.js code snippet:
var config = {
user: 'user',
password: 'password',
server: 'localhost',
database: 'Books',
options: {
instance: 'SQLEXPRESS'
}
};
sql.connect(config, function(err){
console.log(err);
});
bookRoute.js code snippet without callback:
bookRouter.route('/')
.get(function (req, res) {
console.log('book router');
var request = new sql.Request();
request.query('select * from books').then(
function (err, recordset) {
console.log(recordset);
})
.catch(function(err){ console.log(err)});
});
bookRoute.js code snippet with callback:
bookRouter.route('/')
.get(function (req, res) {
console.log('book router');
var request = new sql.Request();
request.query('select * from books',
function (err, recordset) {
console.log(recordset);
});
});
Once a user accesses the webpage, then the console should display the results. Unfortunately, the only result that is shown is undefined when not using a callback.
console output:
P:\ub\lic\library>gulp serve
[11:08:28] Using gulpfile P:\ub\lic\library\gulpfile.js
[11:08:28] Starting 'style'...
[11:08:28] Starting 'inject'...
[11:08:53] Finished 'inject' after 808 ms
[11:08:53] Finished 'style' after 25 s
[11:08:53] Starting 'serve'...
[11:08:53] Finished 'serve' after 5.31 ms
[11:08:53] [nodemon] 1.9.2
[11:08:53] [nodemon] to restart at any time, enter `rs`
[11:08:53] [nodemon] watching: *.js src/**/*.js
[11:08:53] [nodemon] starting `node app.js`
running server on port 3000
null
book router
undefined
[11:09:21] [nodemon] restarting due to changes...
Restarting the server.....beep boop beep beep
[11:09:21] [nodemon] restarting due to changes...
Restarting the server.....beep boop beep beep
[11:09:21] [nodemon] starting `node app.js`
running server on port 3000
null
book router
[ { id: 1,
title: 'A,B,C with Big Bird ',
author: 'Michael Jacob ' },
{ id: 2,
title: 'Peter and his Petunias ',
author: 'Jess Holiday ' },
{ id: 3,
title: 'The Amazing Average Guy ',
author: 'Don Dillon ' } ]
bookRoute.js code snippet without callback:
bookRouter.route('/').get(function (req, res) {
console.log('book router');
var request = new sql.Request();
request.query('select * from books')
.then(function (recordset) {
console.log(recordset);
})
.catch(function (err) {
console.log(err);
});
});
When using bookRoute.js code snippet without callback.The then function should have only one argument that is the result from the query that is how it is stated in the documentation.When there is an error the catch function is called.
In my scenario, node need read a array of img files in a directory, and store binaries to MongoDB. This function is to test the ability of store file to MongoDB. The img file size range 5kb to 3mb.
Part 1. connect mongdb
I use the options parameter to set the socket's keepAlive, I want to the socket keep long.
var options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS : 30000
}
}
};
mongoose.connect(mongooseURI, options);
Part 2. insert operation
fs can read the files successfully, but the number of files may cause a error, i will write lately. I use the collection.insert to store a array of data.
Img.collection.insert(fileArr, function(err, docs){
if(err){
console.info('\033[31mError: Failed to save docs with error: ', err, '\033[39m');
process.emit('DB_OPS_DONE', err.message);
}else{
console.info('%d images were successfully stored.', docs.length);
console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
process.emit('DB_OPS_DONE', 'Successful insert docs');
}
});
Part 3. close connection
This make the node app stop normally whether the error come up.
process.on('DB_OPS_DONE', function(msg) {
mongoose.connection.close(function () {
console.log('Mongoose disconnected on app termination with ', msg);
process.exit(0);
});
});
I get a error if the number of files become large. Number test out about 40.That means if the number of files < 40, the node can finish successfully.
The error is MongoError: server localhost:27017 received an error {"name":"MongoError","message":"read ECONNRESET"}
environment:
os: win7
nodejs: 0.10.31
mongodb: 2.7
mongoose: 4.0.5