Elastic search gives Bad request for ping - node.js

Code in elasticsearch.js file
function es() {
throw new Error('Looks like you are expecting the previous "elasticsearch" module. ' +
'It is now the "es" module. To create a client with this module use ' +
'`new es.Client(params)`.');
}
es.Client = require('./lib/client');
es.ConnectionPool = require('./lib/connection_pool');
es.Transport = require('./lib/transport');
es.errors = require('./lib/errors');
module.exports = es;
var elasticsearch = require('elasticsearch')
var client = new es.Client({
host: 'localhost:9200',
log: 'trace',
})
// Ping the cluster
client.ping({
requestTimeOut: 30000,
},
function(error){
if(error) {
console.log(error)
console.error("elasticsearch cluster is down!")
}
else {
console.log("All is well")
}
})
and I am running elastic search locally with command $bin/elasticsearch
but when I do $node elasticsearch.js it gives the error saying
Elasticsearch INFO: 2018-01-22T11:17:50Z
Adding connection to http://localhost:9200/
Elasticsearch DEBUG: 2018-01-22T11:17:50Z
starting request {
"method": "HEAD",
"requestTimeout": 3000,
"castExists": true,
"path": "/",
"query": {
"requestTimeOut": 30000
}
}
Elasticsearch TRACE: 2018-01-22T11:17:50Z
-> HEAD http://localhost:9200/?requestTimeOut=30000
<- 400
Elasticsearch DEBUG: 2018-01-22T11:17:50Z
Request complete
{ Error: Bad Request
at respond (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:307:15)
at checkRespForFailure (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:266:7)
at HttpConnector.<anonymous> (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
at IncomingMessage.bound (/Users/ElasticSearchServer/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
status: 400,
displayName: 'BadRequest',
message: 'Bad Request',
path: '/',
query: { requestTimeOut: 30000 },
body: undefined,
statusCode: 400,
response: '',
toString: [Function],
toJSON: [Function] }
elasticsearch cluster is down!
If I try adding new index, delete index, check the health or search, it works fine and gives the appropriate result.
Can anyone help me to fix the issue? thanks in advance!

In the new JavaScript client every option that is not intended for Elasticsearch lives in a second object, your code should be updated as follows:
'use strict'
const { Client } = require('#elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
client.ping({}, { requestTimeout: 20000 }, (err, response) => {
...
})
In the response object other than body, statusCode, and headers, you will also find a warnings array and a meta object, which should help you debug issues.
In this case, warnings contained the following message: 'Client - Unknown parameter: "requestTimeout", sending it as query parameter'.

Related

Node JS Events Emitter on net.createserver

So i got a problem and stuck around 3 days, i'm using node js as a socket server to receive string and processing some json data to give a string response to socket client, stack used in node js(net, events), my code have no problem for the first request, but after the second request it got an error like this(server):
[2020-12-23T07:45:13.821Z] server listening on port: 4444
[2020-12-23T07:45:15.696Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:15.701Z] message: string
[2020-12-23T07:45:15.702Z] executing call function
[2020-12-23T07:45:15.703Z] oncall function
[2020-12-23T07:45:15.703Z] executing emit
[2020-12-23T07:45:15.704Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:15.707Z] Returning : diterima
[2020-12-23T07:45:42.403Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:42.407Z] message: string
[2020-12-23T07:45:42.408Z] executing call function
[2020-12-23T07:45:42.408Z] oncall function
[2020-12-23T07:45:42.409Z] executing emit
[2020-12-23T07:45:42.409Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.410Z] Returning : diterima
[2020-12-23T07:45:42.411Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.412Z] Returning : diterima
events.js:288
throw er; // Unhandled 'error' event
^
Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:451:14)
at HanaconsResponded.<anonymous> (C:\Users\ralfian\--\--\--.js:47:11)
at HanaconsResponded.emit (events.js:323:22)
at callFunction (C:\Users\ralfian\--\--\--.js:79:23)
at Socket.<anonymous> (C:\Users\ralfian\--\--\--.js:35:17)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:271:13)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on Socket instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EPIPE'
}
and bellow is error on the client side at second request, (first request have no problem):
CLIENT: I connected to the server.
{"orig":"orig","origParam":{"number":"123456"},"resp":[{"content":[{"gender":"not match"}],"firstPage":true,"lastPage":true,"number":0,"numb
erOfElements":1,"size":1,"sort":null,"totalElements":1},{"result":"UNMATCHED"}]}
events.js:288
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
Here is my server Code:
var net = require('net');
var server = net.createServer();
require('log-timestamp');
var hostport = 4444;
var EventEmitter = require('events');
class CheckResponded extends EventEmitter {}
var checkResponded = new CheckResponded();
server.listen({
port : hostport,
exclusive: true
},);
console.log('server listening on ' + 'port: ' + hostport);
server.on('connection', (e) => {
console.log( 'New Connection : ' + e.remoteAddress );
e.setEncoding('utf8');
e.setTimeout(60000);
e.on('end', () => {})
e.on( 'timeout', () => {
console.log('Socket Timeout. Reseting.');
e.end();
});
e.on( 'data', (buff) => {
console.log("message: " + typeof buff);
try {
if (buff === "exec"){
console.log("executing call function")
callFunction()
}
} catch(error){
console.log('error on socket -> ' + error)
}
});
checkResponded.on( 'event1', (f) => {
console.log(f)
var rmsg;
rmsg = f;
console.log("Returning : " + 'diterima');
e.write(JSON.stringify(rmsg));
});
});
function callFunction() {
console.log('oncall function')
var imsg = {
"orig": "orig",
"origParam": {
"number": "123456",
},
"resp": [
{
"content": [
{
"gender": "not match",
}
],
"firstPage": true,
"lastPage": true,
"number": 0,
"numberOfElements": 1,
"size": 1,
"sort": null,
"totalElements": 1
},
{
"result": "UNMATCHED"
}
]
}
console.log('executing emit')
checkResponded.emit('event1', imsg);
return
}
and bellow is the client request example:
const net = require('net');
var host = 'localhost';
const client = net.createConnection({ port: 4444, host: host }, () => {
console.log('CLIENT: I connected to the server.');
client.write('exec')
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('CLIENT: I disconnected from the server.');
});
looking for help , Thanks!
One thing I see is that your client code assumes that your entire response arrives in one data event. That is not a safe assumption. It may sometimes happen that way, but it is not guaranteed and if the server is not done sending data when you call client.end(), you will get an error (like you are) on the server such as:
Error: This socket has been ended by the other party
You will need some sort of protocol in the data you send so that you can parse the incoming data and you can then know when you have a complete response and when you need to wait for the rest of the data to arrive.
Similar issue discussed here: Detect complete data received on 'data' Event in Net module of Node.js

Node.JS aws-sdk getting "socket hang up" error

I am trying to get the pricing information of AmazonEC2 machines using "#aws-sdk/client-pricing-node" package.
Each time I will send a request to get the pricing information, process it, and send the request again, until all the information is obtained (no NextToken anymore). The following is my code.
const {
PricingClient,
} = require('#aws-sdk/client-pricing-node/PricingClient');
const {
GetProductsCommand,
} = require('#aws-sdk/client-pricing-node/commands/GetProductsCommand');
const agent = new https.Agent({
maxSockets: 30,
keepAlive: true,
});
const pricing = new PricingClient({
region: "us-east-1",
httpOptions: {
timeout: 45000,
connectTimeout: 45000,
agent,
},
maxRetries: 10,
retryDelayOptions: {
base: 500,
},
});
const getProductsCommand = new GetProductsCommand( { ServiceCode: 'AmazonEC2', });
async function sendRequest() {
let result = false;
while (!result) {
try {
const data = await pricing.send(getProductsCommand);
result = await handleReqResults(data);
} catch (error) {
console.error(error);
}
}
}
async function handleReqResults(data) {
// some data handling code here
// ...
//return false when there is "NextToken" in the response data
if (data.NextToken) {
setNextToken(data.NextToken);
return false;
}
return true;
}
The code will run for a while (variable time) and then stop with the following error:
{ Error: socket hang up
at createHangUpError (_http_client.js:332:15)
at TLSSocket.socketOnEnd (_http_client.js:435:23)
at TLSSocket.emit (events.js:203:15)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
code: 'ECONNRESET',
'$metadata': { retries: 0, totalRetryDelay: 0 } }
I had tried to run it on a GCP VM instance, and there was no such a problem. But the problem happens when I run it on my local machine.
Do anyone have any idea how to solve this problem?
(BTW: my node version is v10.20.1)

Error: socket hang up in Http Request made in Node JS using request-promise causing for loop to restart

I am trying to make an Http Request using request-promise inside a for loop. But it seems if a Http Request takes long, request-promise closes the connection.
This behavior is ok but what I am not able to grasp is the for loop starts from 0 again after the error is printed.
Below is the code
const rp = require('request-promise');
async function stepIterator(processingSteps, documentId) {
var finalResult = null;
for (var step = 0, len = processingSteps.length; step < len; step++) {
if (step === 0 || step === 1 || step == 2 || step == 3) {
try {
console.log('Calling step ', step);
let url = 'http://internal-server:8080/process';
let collection = getCollection(documentId);
let splitText = getSPlit(documentId);
let outputFormat = 'xmi';
let documentObject = await callServer(url, collection, splitText, outputFormat);
finalResult = documentObject;
} catch (error) {
console.log("Error");
}
}
}
return finalResult;
}
async function callServer(url, collection, splitText, outputFormat) {
var options = {
method: 'POST',
uri: url,
headers: {
'Content-Type': 'multipart/form-data',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive '
},
formData: {
collection: collection,
text: splitText,
output: outputFormat
}
};
return rp(options)
}
The complete error trace is as follows
{ RequestError: Error: socket hang up
at new RequestError (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:185:22)
at Request.emit (events.js:182:13)
at Request.onRequestError (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:881:8)
at ClientRequest.emit (events.js:182:13)
at Socket.socketOnEnd (_http_client.js:425:9)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) name: 'RequestError', message: 'Error: socket hang up', cause:
{ Error: socket hang up
at createHangUpError (_http_client.js:322:15)
at Socket.socketOnEnd (_http_client.js:425:23)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, error: { Error: socket hang up
at createHangUpError (_http_client.js:322:15)
at Socket.socketOnEnd (_http_client.js:425:23)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, options: { method: 'POST',
uri: 'http://internal-server:8080/process',
json: true,
headers: { Connection: 'keep-alive ' },
body:
{ docSplitId: [Array],
_id: 5c579d84812acb17ec74ac39,
contentType: 'application/pdf',
location:
'C:\\Users\\newuser\\AppData\\Local\\Temp\\2\\report.pdf',
docModelVersion: '1',
visualMetaDataId: null,
categoryId: '5c52a72f6df294140c0535bc',
deductedInfo: null,
status: 'New',
isDeleted: false,
metadata: [Object],
detailedStatus: [Array] },
callback: [Function: RP$callback],
transform: undefined,
simple: true,
resolveWithFullResponse: false,
transform2xxOnly: false }, response: undefined }
Obviously the socket is hanging! Don't bother with http, it is a little complex. Use node unirest and it closes the data stream.
var unirest = require('unirest');
var req = unirest('POST', 'localhost:3200/store/artifact/metamodel')
.attach('file', '/home/arsene/DB.ecore')
.field('description', 'We are trying to save the metamodel')
.field('project', '6256d72a81c4b80ccfc1768b')
.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.raw_body);
});
Hope this helps!

GraphQL Error Handling with ApolloClient and Apollo-Server-Express

The onError function from apollo-link-error has a populated graphQLErrors Object, but when ApolloClient throws an error object, the graphQLErrors property contains an empty array. Further inspection reveals the graphql error message in error.networkError.result.errors.
How can one properly configure Apollo Client to return a populated graphQLErrors object?
Apollo Client Setup:
const {ApolloClient} = require('apollo-client')
const { ApolloLink } = require('apollo-link')
const {HttpLink} = require('apollo-link-http')
const {onError} = require('apollo-link-error')
const {InMemoryCache} = require('apollo-cache-inmemory')
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
)
}
if (networkError) console.log(`[Network error]: ${networkError}`)
})
const middleware = (req, ignored_res, next) => {
const client = new ApolloClient({
link: ApolloLink.from([errorLink, new HttpLink({ uri:'http://somegraphqlserver.com', fetch: require('node-fetch') })]),
cache: new InMemoryCache(),
})
req.GQLClient = client
return next()
}
module.exports = middleware
Calling apollo-server-express:
req.GQLClient
.query({
query: SOME_MALFORMED_QUERY,
})
.then((data) => {...})
.catch((error) => {
console.log('rawError', error)
console.log('error.networkError.result.errors', error.networkError.result.errors)
return next(error)
})
Console Results:
[GraphQL error]: Message: Cannot query field "blah" on type "CustomerList"., Location: [object Object], Path: undefined
[Network error]: Error: Response not successful: Received status code 400
rawError { Error: Network error: Response not successful: Received status code 400
at new ApolloError (/.../node_modules/apollo-client/bundle.umd.js:121:28)
at /.../node_modules/apollo-client/bundle.umd.js:1187:41
at /.../node_modules/apollo-client/bundle.umd.js:1620:17
at Array.forEach (<anonymous>)
at /.../node_modules/apollo-client/bundle.umd.js:1619:18
at Map.forEach (<anonymous>)
at QueryManager.broadcastQueries (/.../node_modules/apollo-client/bundle.umd.js:1614:22)
at /.../node_modules/apollo-client/bundle.umd.js:1114:31
at process._tickCallback (internal/process/next_tick.js:178:7)
graphQLErrors: [],
networkError:
{ Error: Response not successful: Received status code 400
at throwServerError (/.../node_modules/apollo-link-http-common/lib/bundle.umd.js:33:21)
at /.../node_modules/apollo-link-http-common/lib/bundle.umd.js:58:17
at process._tickCallback (internal/process/next_tick.js:178:7)
response:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: [Object],
[Symbol(Response internals)]: [Object] },
statusCode: 400,
result: { errors: [Array] } },
message: 'Network error: Response not successful: Received status code 400',
extraInfo: undefined }
error.networkError.result.errors [ { message: 'Cannot query field "blah" on type "CustomerList".',
locations: [ [Object] ] } ]
library versions:
Server:
"apollo-server-express": "^1.3.2"
Client:
"apollo-cache-inmemory": "^1.1.12"
"apollo-client": "^2.2.8"
"apollo-link-error": "^1.0.9"
"apollo-link-http": "^1.5.4"
This is by design. From the docs:
graphQLErrors: An array of errors from the GraphQL endpoint
networkError: Any error during the link execution or server response,
that wasn't delivered as part of the errors field in the GraphQL result
In other words, if your query is malformed, you request a field that isn't valid (like in your example), or hit any other issue that results in status other than 200, the error will appear as part of the networkError property. On the other hand, if the request returns a 200, but the errors array inside the response is populated, those same errors will be returned as part of graphQLErrors.
If you want to see an example of graphQLErrors being populated, format your query correctly but have one of your resolvers throw an error as soon as it's called. As long as the query doesn't hit any other issues, you should see the same error pop up inside graphQLErrors.

How should I do to make an elasticSearch , search query from Node.js correctly?

I am stuck in a problem and can not figure out the solution.
I want to use an elasticSearch query from my nodejs.
The problem is, I can make it work from postman, but not from node.
http://user:psd#my_domain:9200/ra_autocomplete/search
And from my nodejs app :
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'my_domain',
port : 9200,
protocol : 'http',
auth : 'user:psd',
maxRetries : 2
});
And then,
client.search({
index: "ra_autocomplete",
body: {
query: {
m_prefix : {
r_n : {
query : my_var
}
}
}
}
} , function(err, res) {
console.log(err);
console.log(res);
});
I get this error :
Error: Not Found
at respond (my_path\node_modules\elasticsearch\src\lib\transport.js:307:15)
at checkRespForFailure
(my_path\node_modules\elasticsearch\src\lib\transport.js:266:7)
at HttpConnector.
(my_path\node_modules\elasticsearch\src\lib\connectors\http.js:159:7)
at IncomingMessage.bound
(my_path\node_modules\lodash\dist\lodash.js:729:21)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
status: 404,
displayName: 'NotFound',
message: 'Not Found',
path: '/roads_autocomplete/_search',
query: {},
body: '{"query":{"m_prefix":{"r_n":{"query":"montexte a analyser"}}}}',
statusCode: 404,
response: '\r\n404 Not Found\r\n\r\n404 Not Found\r\nnginx/1.10.3 (Ubuntu)\r\n\r\n\r\n',
toString: [Function],
toJSON: [Function] }
Any help would be appreciated, the problem is, when I try to make it with postman, it goes well.
Thank you.
I think the problem is that you are not connecting to elastic search node.
Add your port and domain to into one line and then start by running a simple query.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.search({
index: 'products',
type: 'product',
body: {
query: {
bool: {
}
}
}
}).then((body) => {
return body;
}, (error) => {
console.trace(error.message);
});

Resources