While working on some new (very simple) server code, I connected and inserted MongoDB URL code in order to "deploy" it on MongoDB Atlas.
However, I get the following warning:
(node:3396) DeprecationWarning: current URL string parser is
deprecated, and will be removed in a future version. To use the new
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
This doesn't prevent my code from deploying to the server, and still allows me to send requests with a local testing tool (Postman). However, after my requests are sent, I get the following message in VS Code:
{ MongoError: user is not allowed to do action [insert] on [agenda.contacts]
at Connection.<anonymous> (C:\Users\mymachine\agenda\node_modules\mongodb-core\lib\connection\pool.js:443:61)
at Connection.emit (events.js:198:13)
at processMessage (C:\Users\mymachine\agenda\node_modules\mongodb-core\lib\connection\connection.js:364:10)
at TLSSocket.<anonymous> (C:\Users\mymachine\agenda\node_modules\mongodb-core\lib\connection\connection.js:533:15)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
ok: 0,
I suspect this is probably a version error, however this is out of my area of expertise.
My setup is very simple and has only the Atlas cluster connected on MongoDB website.
MongoDB still provides all the analytics I need, so it doesn't seem to have a problem there.
I've already tried changing mongoClient.connect code from useMongoClient: true to useNewUrlParser: true, but it only changes the warning message code from (node:3396) to (node:1092).
mongoose.connect(
'*inserted mongodb url code, copied from CONNECTION STRING ONLY");
{
useNewUrlParser: true
}
Since I have never used MongoDB, I believe my issue is on that software.
I copied the CONNECTION STRING ONLY instead of FULL DRIVER EXAMPLE (both provide a code to insert into my code in VS Code.
As to the (node(1234)) issue solved above, it was in regards to a Warning about a newer version, as to call useNewUrlParser: trueinstead of useMongoClient: true.
As to the "MongoError", I was, instead of hardcoding my password, calling a + process.env.MONGO_ATLAS_PW +and sending my password to file ./nodemon.json.
After changing both errors, code ran smoothly. I, however, will have to find a new way to send password, instead of hardcoding it.
Thank you all who has helped.
Related
At some random times, on our production server, we face such type of timeouts and the server crashes.
Error: ESOCKETTIMEDOUT
at ClientRequest.<anonymous> (/usr/src/app/node_modules/request/request.js:816:19)
at Object.onceWrapper (events.js:272:13)
at ClientRequest.emit (events.js:180:13)
at ClientRequest.emit (domain.js:422:20)
at TLSSocket.emitTimeout (_http_client.js:703:34)
at Object.onceWrapper (events.js:272:13)
at TLSSocket.emit (events.js:180:13)
at TLSSocket.emit (domain.js:422:20)
at TLSSocket.Socket._onTimeout (net.js:396:8)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
I have gone though a lot of blogs and QAs. To summarize, it's read timeout when the caller doesn't get next bytes of response within some specific time limit. From the offical docs (request-npm), we can see -
Read timeout: Time to wait for a server to send response headers (and start the response body) before aborting the request.
Connection timeout: Sets the socket to timeout after timeout milliseconds of inactivity. Note that increasing the timeout beyond the OS-wide TCP connection timeout will not have any effect (the default in Linux can be anywhere from 20-120 seconds)
However I am trying to reproduce it in local linux machine with a HTTP request to my own Node JS server. I am able to get ETIMEDOUT which is connection timeout but I can't get ESOCKETTIMEDOUT error reproduced. In my destination server I had used this simple script with Express JS as an attempt to get this error.
app.use((req, res, next) => {
res.write('sample string');
// Do nothing, let the req wait
});
Therefore does anybody knows a simple approach to get this reproduced? Also I would like to know how to handle this error instead of having it as uncaught exception.
Thanks!
I have a use case where I need to fetch data from two different DBs placed on different server(one of them being my own localhost/dev/prod). On providing details for connection using adapter : 'sails-mysql', whenever i try & access the DB(for a simple select query), it throws this exception:
debug: err, response -- Error: Could not connect to MySQL:
Error: Handshake inactivity timeout
at afterwards (/Users/xx/code/xx/xx/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
at Handshake.Sequence.end (/Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:399:18
at Array.forEach (<anonymous>)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:398:13
at process._tickCallback (internal/process/next_tick.js:112:11) undefined
Example of connections.js:
MySQLServerForDataServer: {
adapter: 'sails-mysql',
host: (process.env.DBNAME_DATA_SERVER_HOST),
user: (process.env.DBNAME_DATA_SERVER_USER),
password: (process.env.DBNAME_DATA_SERVER_PWD),
database: (process.env.DBNAME_DATA_SERVER_DB)
}
I'm able to access this server through Valentina Studio or whatever(ssh key is added on there server too), but through code, it's not letting me through.
Can anyone suggest something which might help me connect to this DB Server?
My customer is all of a sudden experiencing problems with a HTML scraper job made with Node.js. I have circled in on the cause, and found that it's located in the Request module. That made me write a small test application, which solely gets the HTML of the given URL via the Request module. Like this:
var request = require('request');
request('https://www.politi.dk/da/ompolitiet/jobipolitiet/ledige_stillinger/ledigestillinger', function(err, res, body){
if(err){
console.log(err);
} else {
console.log('statusCode:', res.statusCode);
console.log('statusMessage:', res.statusMessage);
}
});
The above example does not work though, as I am getting the following error when running the application:
{ Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1137:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
code: 'ECONNRESET',
path: null,
host: 'www.politi.dk',
port: 443,
localAddress: undefined }
However if I change the URL to any other URL it works and I get the following:
statusCode: 200
statusMessage: OK
I have tried passing other URL's on the politi.dk domain, which doesn't work either. Therefore I can conclude that there's a problem with this domain, when requesting pages via the Request module. The strange thing is just, that it worked up until recently. What can cause this problem? Can some changes in settings be made to the server of politi.dk, that is causing this now? I find it hard to find anything helpful on Google. I found the nodejs-what-does-socket-hang-up-actually-mean thread here on SO, which is the exact same problem. But the answers doesn't help me much.
Anyone?
Background
Trying to connect to my Apache ActiveMQ broker using the AMQP 1.0 protocol via amqp10. I am using the following code (adapted from the original example in the README):
const AMQPClient = require("amqp10").Client;
const Promise = require("bluebird");
//Fix from: https://github.com/noodlefrenzy/node-amqp10/issues/241
const activeMQPolicy = require("amqp10").Policy;
const client = new AMQPClient(activeMQPolicy.ActiveMQ);
const setUp = () => {
return Promise.all([
client.createReceiver("amq.topic"),
client.createSender("amq.topic")
]);
};
client.connect("amqp://localhost")
.then(setUp)
.spread(function (receiver, sender) {
receiver.on("errorReceived", function (err) {
if (err) {
console.log(`failed with error: ${err}`);
return;
}
receiver.on("message", message => console.log(`Rx message: ${message.body}`));
return sender.send({ key: "Value" });
});
})
.error( err => console.log("error: ", err));
Error
Execution fails with the following application error:
Workspace/activemq/node_modules/amqp10/lib/frames.js:64
stream.write(buffer, callback);
^
TypeError: Cannot read property 'write' of null
at Object.frames.writeFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/frames.js:64:9)
at Connection.sendFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:329:10)
at Connection._sendCloseFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:491:8)
at Connection._receiveAny (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:413:12)
at Connection._receiveData (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:358:8)
at NetTransport. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:516:38)
at emitOne (events.js:96:13)
at NetTransport.emit (events.js:191:7)
at Socket. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/transport/net_transport.js:26:49)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:560:20)
While ActiveMQ rejects the request with the following message:
WARN | Connection attempt from non AMQP v1.0 client. AMQP,0,1,0,0
WARN | Transport Connection to: tcp://127.0.0.1:53188 failed: org.apache.activemq.transport.amqp.AmqpProtocolException: Connection from client using unsupported AMQP attempted
What I tried
I tried to implement the fix described in this issue:
https://github.com/noodlefrenzy/node-amqp10/issues/241
But it didn't work either. Also tried specifying the port 5672 in the url, but with no luck.
Question
At this point I am quite sure the application fails the write, because ActiveMQ rejects the request as invalid.
Any ideas on how to fix this?
Solution
After asking around in the ActiveMQ forums, it does turn out that #Tim was correct. To quote the user Gordon Sim-2:
As of 5.14 (...) ActiveMQ requires a SASL layer, even if you use
ANONYMOUS to authenticate. The examples for clients often don't
explicitly authenticate in any way (...)
To fix this there are two options.
Option 1 - change code
The first one is to change the code, which is what I was looking for:
To have the amqp10 client use a SASL layer, just specify a
saslMechanism override on the connect e.g.
client.connect(uri, {'saslMechanism':'ANONYMOUS'})
This approach was suggested by the creator of yet another AMQP 1.0 Node.js library called rhea and I highly recommend that if you are reading this, to check it.
Option 2 - change broker config files
The second one, as Tim suggested, is to change the broker configuration files. I recommend reading his post if you opt for this approach as it is more complete.
The most likely issue is that the client is not using a SASL based authentication request on the connect and is just trying to connect directly. The broker defaults to requiring the client be using SASL in it's opening handshake even if the authentication mechanism is only 'Anonymous' so that the client can be validated against any broker security policy that is in place.
You can disable this check by adding wireFormat.allowNonSaslConnections=true to the broker's AMQP TransportConnector configuration and then see if you can connect. This might be a short term solution to get you going however on a production broker you should not be using that option and should have a valid security policy in place with user / password authentication.
Read about broker configuration here.
I've read all the related questions to this one on here, I've searched through Github issues and I've read as much as possible about Express to try and figure this one out. As far is I've read most people say it is something to do with the headers being set after they've been sent usually with res.set() or something similar but I'm not doing that anywhere in my code.
This error only occurs shortly after I've deployed my SailsJS app to Modulus:
Error occurred in session middleware :: 'Error: Error getting collection: sessions\n at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:160:19\n at Db.collection (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:524:27)\n at MongoStore._get_collection (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:158:17)\n at MongoStore.set (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:274:12)\n at Session.save (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/session/session.js:63:25)\n at ServerResponse.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/index.js:263:21)\n at ServerResponse.res.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/compression/index.js:116:15)\n at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:175:8)\n at ServerResponse.res.json (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:223:15)\n at storedUrlCheckServiceDone (/mnt/data/2/api/controllers/ProcessController.js:44:18)\n at tryCatcher (/mnt/data/2/node_modules/bluebird/js/main/util.js:24:31)\n at Promise._settlePromiseFromHandler (/mnt/data/2/node_modules/bluebird/js/main/promise.js:454:31)\n at Promise._settlePromiseAt (/mnt/data/2/node_modules/bluebird/js/main/promise.js:530:18)\n at Promise._settlePromises (/mnt/data/2/node_modules/bluebird/js/main/promise.js:646:14)\n at Async._drainQueue (/mnt/data/2/node_modules/bluebird/js/main/async.js:175:16)\n at Async._drainQueues (/mnt/data/2/node_modules/bluebird/js/main/async.js:185:10)'
http.js:690
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.res.setHeader (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:133:22)
at ServerResponse.res.set.res.header (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:577:10)
at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:142:12)
at Object.<anonymous> (/mnt/data/2/node_modules/sails/lib/hooks/http/middleware/defaults.js:56:24)
at Object.immediate._onImmediate (timers.js:372:16)
at processImmediate [as _immediateCallback] (timers.js:354:15)
[2015-04-13T03:23:03.264Z] Application CRASH detected. Exit code 8.
It's something to do with the sessions and connect-mongo but I have no idea how to figure it out. Usually it happens once or twice straight after deployment but sometimes it'll happen up to 5 or 6 times in a row straight after deployment as well.
I've also seen this error occasionally as well around the same time so it might be related:
/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
Error: Error setting TTL index on collection : sessions
at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:169:23
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1497:46
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1630:20
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:170:22
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:39
at Cursor.close (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:1009:5)
at commandHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:21)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
at MongoReply.parseBody (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
at emit (events.js:95:17)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
at emit (events.js:98:17)
at Socket.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:440:22)
[2015-04-13T03:24:15.011Z] Application CRASH detected. Exit code 8.
[2015-04-13T03:24:22.048Z] Application restarted with pid 14613
I'm using Node v0.10.37, Sails v0.10.5 and Passport for authentication and just the mongo adapter for session storage.
I'd really appreciate any help or insights you can give as I've spent nearly a week trying to figure this out before posting here and even had Newrelic's support team looking at and they couldn't figure it out either.
Thanks
Update
I'm not seeing this error locally, only on Modulus.
This is a shortened down version of my controller.
I basically have a Auth Service that checks if the users is authenticated, if they are I pass the results off to another promise which checks the db for some of their data, if it find a record then it returns it if not then it calls a 3rd party API and does some processing, once thats done it returns and creates a new record, then returns the results back to the browser. If they aren't authenticated then I send back an error.
There are 4 calls to different Sails services in total in the controller, which all return a promise.
text: function (req, res) {
// Check that the host is authenticated
AuthService.hostname(req).done( authServiceDone, authServiceFail );
function authServiceDone (site) {
// Check the DB for the origin
return urlService.check(req, site.owner).done(UrlCheckServiceDone, UrlCheckServiceFail );
}
function authServiceFail (error) {
return res.json({
error : error
});
}
}