Loopback 3 with loopback-component-storage hang on POST file - node.js

I've a very simple server running under loopback 3 and seeing this issue again: https://github.com/strongloop/loopback-component-storage/issues/9
As the gists and examples don't work any longer I would like to reopen that issue.
I've created a very simple API templated server and added the following code:
module.exports = function (File) {
File.upload = function (ctx, options, cb) {
if (!options) options = {};
ctx.req.params.container = 'common';
console.log("DO");
File.app.models.Storage.upload(ctx.req, ctx.result, options, function (err, fileObj) {
console.log("FILE");
cb(fileObj);
});
};
File.remoteMethod(
'upload',
{
description: 'Uploads a file',
accepts: [
{arg: 'ctx', type: 'object', http: {source: 'context'}},
{arg: 'options', type: 'object', http: {source: 'query'}}
],
returns: {
arg: 'fileObject', type: 'object', root: true
},
http: {verb: 'post'}
}
);
};
The problem is now, that while posting via POSTMAN to the upload function, I see the following behavior in the console:
DO
Error: Request aborted
at IncomingMessage.<anonymous> (...../server/node_modules/formidable/lib/incoming_form.js:120:19)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:188:7)
at abortIncoming (_http_server.js:383:9)
at socketOnClose (_http_server.js:377:3)
at emitOne (events.js:101:20)
at Socket.emit (events.js:191:7)
at TCP._handle.close [as _onclose] (net.js:504:12)
FILE
And Postman returns an empty response...
I'm totally lost as a beginner here at this step!
What is my fault here?
Thx for any input

OMG, it seems that I've found the answer:
https://github.com/strongloop/loopback-component-storage/issues/86
Just use Header "Accept" with value "multipart/form-data", not setting "Content-type". That works for me from Postman

Related

NodeJS - Error (response 408) when downloading a pdf with node-downloader-helper

I want to download pdf with nodeJS, so I created a function with node-downloader-helper module (see below). This function is called multiples time to download a list of pdf.
function downloadPDF(url, dirname, index) {
const options = {
method: "GET", // Request Method Verb
headers: {}, // Custom HTTP Header ex: Authorization, User-Agent
fileName: `document${index}.pdf`, // Custom filename when saved
forceResume: false, // If the server does not return the "accept-ranges" header, can be force if it does support it
removeOnStop: true, // remove the file when is stopped (default:true)
removeOnFail: true, // remove the file when fail (default:true)
httpRequestOptions: {}, // Override the http request options
httpsRequestOptions: {
timeout: 10000, // Timeout in milliseconds
}, // Override the https request options, ex: to add SSL Certs
timeout: 10000, // Timeout in ms, 0 to disable (default:0)
};
const dl = new DownloaderHelper(encodeURI(url), dirname, options);
dl.on('end', () => console.log('Download Completed'));
dl.on('error', (err) => console.log('Download Failed', err));
dl.start().catch(err => console.error(err));
}
When I run my program I get this error : (and sometimes with server completely crash). I think it’s about a timeout error but I tried different methods but none worked.
Error: Response status was 408
at ClientRequest.<anonymous> (/home/ubuntu/CPE-Colle/node_modules/node-downloader-helper/dist/index.js:1:7055)
at Object.onceWrapper (node:events:642:26)
at ClientRequest.emit (node:events:527:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:631:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
at TLSSocket.socketOnData (node:_http_client:494:22)
at TLSSocket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10) {
status: 408,
body: ''
}
I think found the solution, my puppeteer browser that was fetching pdf links wasn't closing so after a while my server crashed.

sending email failed from NodeJS

I am trying to send mail from my NodeJS backend application, but am getting some error :-
Error: self signed certificate in certificate chain
at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:932:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12) {
code: 'ESOCKET',
command: 'CONN'
How can I fix this? The same function is working in some other machines (in local) I think am missing something to configure or install..!
sendMail() {
try {
let info = await this.transporter.sendMail({
from: 'sender#address.com',
to: user.email,
subject: "Subject",
html: `<p>mail content</p>`,
attachments: [{
filename: 'logo.png',
path: join(__dirname, '..images/logo.png'),
}]
});
} catch (error) {
return console.error(error);
}
}

Creating ConverationPost in discussion using rally-node

How do I go about creating a ConverationPost in an existing user story?
I can't seem to get the syntax right:
const create = {
type: 'ConversationPost',
data: {
Text: 'api test comment',
Artifact: '/hierarchicalrequirement/287838839156'
}
};
restApi.create(create)
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
})
The error is always:
{ Error: /ConversationPost/create: 401! body=undefined
at generateError (/Users/ts/Sites/node/tests/node_modules/rally/dist/request.js:38:11)
at Request._callback (/Users/ts/Sites/node/tests/node_modules/rally/dist/request.js:114:20)
at Request.self.callback (/Users/ts/Sites/node/tests/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/Users/ts/Sites/node/tests/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at Gunzip.<anonymous> (/Users/ts/Sites/node/tests/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30) errors: [ '/ConversationPost/create: 401! body=undefined' ] }```
When I looked at dev tools whilst manually creating a post using the Rally GUI, I see this in my request body:
{
"ConversationPost": {
"Text": "testing",
"Artifact": "/hierarchicalrequirement/287838839156"
}
}
Anyone know how to go about this?
EDIT:
Setting request debug to true shows this in the request, which looks correct:
json:
{ ConversationPost:
{ Text: 'api test comment',
Artifact: '/hierarchicalrequirement/287838839156' } },
gzip: true,
url: 'https://rally1.rallydev.com/slm/webservice/v2.0/ConversationPost/create',
qs: {},
callback: [Function],
method: 'POST' }
I should add the 401 seems odd, since I'm able to query against this user story.
I also noticed the request from the rally node module is a POST request, yet in my browser the GUI does a PUT request?
Turns out this was correct. Annoying small print is that the "ALM WSAPI Read-only" makes Rally ignore the second setting, when configuring your API key in rally.
401 error corresponds with "access denied", aka something wrong with key or key permissions.

Elastic search gives Bad request for ping

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'.

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