Problem using websockets from docker container and node - node.js

I have node running from a docker container and I tried to write some websocket example. But I an getting the next problem:
root#7cd5a41c8eea:/application/webSocketEx# node
> var connection = new WebSocket('ws://html5rocks.websocket.org/echo', ['soap', 'xmpp']);
ReferenceError: WebSocket is not defined
> const WebSocket = require('ws');
undefined
> var ws = new WebSocket("ws://www.websocket.org");
undefined
> { Error: Unexpected server response: 404
at ClientRequest.req.on (/application/webSocketEx/node_modules/ws/lib/websocket.js:579:7)
at ClientRequest.emit (events.js:182:13)
at ClientRequest.EventEmitter.emit (domain.js:460:23)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:555:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
at Socket.socketOnData (_http_client.js:441:20)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:460:23)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
domainEmitter:
WebSocket {
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
members: [] },
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
readyState: 2,
protocol: '',
_binaryType: 'nodebuffer',
_closeFrameReceived: false,
_closeFrameSent: false,
_closeMessage: '',
_closeTimer: null,
_closeCode: 1006,
_extensions: {},
_receiver: null,
_sender: null,
_socket: null,
_bufferedAmount: 0,
_isServer: false,
_redirects: 0,
url: 'ws://www.websocket.org',
_req:
ClientRequest {
domain: [Domain],
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header:
'GET / HTTP/1.1\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: 2c4HivAKv2OJ6rO7qYyaNA==\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\nHost: www.websocket.org\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: undefined,
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/',
_ended: false,
res: [IncomingMessage],
aborted: 1563389938612,
timeoutCb: null,
upgradeOrConnect: false,
parser: [HTTPParser],
maxHeadersCount: null,
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] } },
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] },
domainThrown: false }
>
Is there a configuration parameter I am missing to use websockets from a docker container and node? I installed ws using npm install ws already. I have the feeling I need to open a port for the docker container but I do not understand which one.
Thanks

Docker container runs on their own subnet and therefore they are not allowed to access any other hosts from it. You need to explicitly change your docker subnet to your host subnet (bridging network) and once your docker container is on the same network as host your container will be able to access internet.
You need to implement something like this.
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
version: '2'
services:
web:
image: some/image
networks:
- dockernet
networks:
dockernet:
external: true
For more information you can read the document here for your specific use case:
https://docs.docker.com/network/network-tutorial-standalone/
https://technology.amis.nl/2018/08/18/docker-host-and-bridged-networking-running-library-httpd-on-different-ports/

Related

How do i connect a simple node.js app running on docker to a Open Policy Server also running on a docker container

I'm trying to bundle together a node app and a Open Policy Agent server using docker compose, i can access the OPA server from my browser but when i tried to to a request using axios from the node app i get this:
Error: connect ECONNREFUSED 127.0.0.1:8181
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8181,
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.26.0'
},
method: 'get',
url: 'http://localhost:8181',
data: undefined
},
request: <ref *1> Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false
},
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError],
socket: [Function: handleRequestSocket]
},
_eventsCount: 3,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: '/',
method: 'GET',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'localhost',
port: '8181',
nativeProtocols: [Object],
pathname: '/'
},
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
_header: 'GET / HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/0.26.0\r\n' +
'Host: localhost:8181\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'GET',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'localhost',
protocol: 'http:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
_currentUrl: 'http://localhost:8181/',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
I also tried to run 2 node app with the same results so the opa servers is no the problem
I created an network and make sure both containers are part of it:
$ docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' opa_network
result: opa-nodejs_client_1 opa-nodejs_server_1
$ docker exec opa-nodejs_client_1 ping opa-nodejs_server_1 -c2
Result: PING opa-nodejs_server_1 (172.31.0.3) 56(84) bytes of data.
64 bytes from opa-nodejs_server_1.opa_network (172.31.0.3): icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from opa-nodejs_server_1.opa_network (172.31.0.3): icmp_seq=2 ttl=64 time=0.128 ms
--- opa-nodejs_server_1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1023ms
rtt min/avg/max/mdev = 0.065/0.096/0.128/0.033 ms
this is the docker compose file:
version: '3.9'
services:
server:
build:
dockerfile: Dockerfile
context: ./OPA
ports:
- 8181:8181
networks:
- opa_network
client:
build:
dockerfile: Dockerfile
context: ./nodejs_google_auth
ports:
- 3000:3000
networks:
- opa_network
networks:
opa_network:
external: true
thanks, i found the solution, each docker container has its own ip address, so i had to use the ip address of the server container instead of "localhost"

Axios Typescript, fetching from openweathermap api causes bad request or ECONNREFUSED 127.0.0.1:80

Im having issues connecting to openweathermaps API in my express app,
app.get("/", cors(), async (req, res) => {
const lat: number = + req.query.lat;
const lon: number = + req.query.lon;
if (lat && lon) {
const entry = cach.filter(e => e.lat === lat && e.lon === lon);
if (entry.some(e => (Date.now() - e.last_updated < 36000000))) {
res.json(entry[0]);
} else { // no entry found
res.json(await getWeather(lat, lon));
}
}
res.send("no location specified")
});
app.listen(PORT, function () {
console.log(`App is listening on port ${PORT}`); //5000 by default
});
to fetch the data im using axios:
async function getWeather(lat: number, lon: number) {
try {
const res = await axios({
url:`http:api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${process.env.WAPI}`,
method:"GET"
});
const data = res.data;
...
catch (error){
console.log(error)
}
but the response i get is:
App is listening on port 5000
at Wed Dec 09 2020 15:22:21 GMT+0100 (Central European Standard Time): http://localhost:5000/?lat=65.58415&lon=22.15465: GET request
Error: connect ECONNREFUSED 127.0.0.1:80
>at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16)
{
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config: {
url: 'http:api.openweathermap.org/data/2.5/weather?lat=65.58415&lon=22.15465&appid=--apiket--',
method: 'get',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.21.0'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
data: undefined
},
request: <ref *1> Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
constructed: true,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false,
closeEmitted: false,
[Symbol(kOnFinished)]: []
},
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError]
},
_eventsCount: 2,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: 'api.openweathermap.org/data/2.5/weather?lat=65.58415&lon=22.15465&appid=--apikey--',
method: 'GET',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: null,
port: null,
nativeProtocols: [Object],
pathname: 'api.openweathermap.org/data/2.5/weather',
search: '?lat=65.58415&lon=22.15465&appid=--apikey--'
},
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
_closed: false,
socket: [Socket],
_header: 'GET api.openweathermap.org/data/2.5/weather?lat=65.58415&lon=22.15465&appid=--apikey-- HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/0.21.0\r\n' +
'Host: localhost\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'GET',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: 'api.openweathermap.org/data/2.5/weather?lat=65.58415&lon=22.15465&appid=--apikey--',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'localhost',
protocol: 'http:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
_currentUrl: 'http:api.openweathermap.org/data/2.5/weather?lat=65.58415&lon=22.15465&appid=--apikey--',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
node:internal/process/promises:225
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:278:15)
at ServerResponse.setHeader (node:_http_outgoing:563:11)
at ServerResponse.header (/home/johanrh/Documents/Git/M7011E_AJ/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/home/johanrh/Documents/Git/M7011E_AJ/node_modules/express/lib/response.js:170:12)
at /home/johanrh/Documents/Git/M7011E_AJ/build/src/Weather-Module/index.js:32:9
at processTicksAndRejections (node:internal/process/task_queues:93:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
the provided url path when put in the browser causes no issus, Anyone have any ideas?

Fix Axios Error ECONNREFUSED on local domain with no proxy

I'm bumping into this stupid issue with Axios.
I'm trying to send a local POST request to my basic local Symfony app with the following piece of code that is a nightmare JS crawler:
Yuzu.fun is my local dev domain with an entry in /etc/hosts to resolve it, the app is running on port 80.
await axios.request({
url: 'http://yuzu.fun/app_dev.php/api/path/to/endpoint',
headers: {
'key': 'xxxxxx'
},
method: 'post',
proxy: false,
data: {
image_data: data
}
})
.then(function (response) {
map_hash = response.message;
console.log('buildMap', map_hash);
})
.catch(function (error) {
console.error('upload failed:', error);
});
When i run my script, here is the error i get:
upload failed: { Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
key: 'xxxxx',
'User-Agent': 'axios/0.18.0',
'Content-Length': 4173 },
method: 'post',
url: 'http://yuzu.fun/app_dev.php/api/path/to/endpoint',
proxy: false,
data: '{"image_data":"imagedatainbase64"}' },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain: null,
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: '/app_dev.php/api/path/to/endpoint',
method: 'post',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: 'yuzu.fun',
port: null,
nativeProtocols: [Object],
pathname: '/app_dev.php/api/path/to/endpoint' },
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 4173,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /app_dev.php/api/path/to/endpoint HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nkey: xxxxx\r\nUser-Agent: axios/0.18.0\r\nContent-Length: 4173\r\nHost: yuzu.fun\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/app_dev.php/api/path/to/endpoint',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http://yuzu.fun//app_dev.php/api/path/to/endpoint' },
response: undefined }
I tried to post to a random endpoint to check if the connection was working when reaching a live domain and it's OK but it doesn't work locally.
When i check stackoverflow or forums i see people mentioning proxies issues so i forced it to false but i don't think that something i'm concerned with.
This code was first written with request.post() but i need to have Axios Promises benefits. When the code was written with request, the local domain was reachable correctly. So i exclude the fact that nightmare could interfere with the domain resolution.
Does anyone have any idea of was could go wrong here?
Thanks in advance!

Can't upload to google cloud or firebase storage bucket from nodejs

I have been trying to upload some images to my firebase storage bucket .
I have followed this official docs :-
https://firebase.google.com/docs/storage/admin/start
and https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/latest/storage/bucket
I am able to see the files which are already present in the bucket (these files were stored there using an android app)
But I'm not able to upload local files from nodejs's admin.storage module .
Here is the code :
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myprojectid.firebaseio.com/" ,
storageBucket : "myprojectid.appspot.com/"
});
bucket = admin.storage().bucket() ;
bucket.upload('./mylocalfile.jpg' , (err , file , response)=>{
console.log(err) ; //always gives me err
console.log(file) ; //gives undefined
}) ;
But bucket.getFiles().then(objects=>console.log(objects)) ; works perfectly and prints the files present in the root .
How to fix this ?
Here is the error it prints when console logging err :
{ ApiError: Not Found
at Object.parseHttpRespMessage (C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\#google-cloud\common\src\util.js:156:33)
at Object.handleResp (C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\#google-cloud\common\src\util.js:131:18)
at C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\#google-cloud\common\src\util.js:465:12
at Request.onResponse [as _callback] (C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\retry-request\index.js:179:7)
at Request.self.callback (C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\request\request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\Users\Natesh\Desktop\AttentionPlease_backend\functions\node_modules\firebase-admin\node_modules\request\request.js:1163:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: 404,
errors: [],
response:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: true,
endEmitted: true,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events:
{ end: [Object],
close: [Object],
data: [Function],
error: [Function] },
_eventsCount: 4,
_maxListeners: undefined,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 9,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'www.googleapis.com',
_readableState: [Object],
readable: true,
domain: [Object],
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 4047,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: null,
read: [Function],
_consuming: true,
_idleTimeout: -1,
_idleNext: null,
_idlePrev: null,
_idleStart: 1307656 },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 9,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'www.googleapis.com',
_readableState: [Object],
readable: true,
domain: [Object],
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 4047,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: null,
read: [Function],
_consuming: true,
_idleTimeout: -1,
_idleNext: null,
_idlePrev: null,
_idleStart: 1307656 },
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers:
{ 'x-guploader-uploadid': 'AEnB2UqADzuHb4O7UjdBAui1cDWeLNO4s0YuT2krCPoYIHaUrYPjXRH8rBU0mcSi9n7sie11PhALTN2vOKkhykW0apqTbrNB9Q',
vary: 'Origin, X-Origin',
'content-type': 'text/html; charset=UTF-8',
'content-length': '9',
date: 'Sat, 17 Mar 2018 13:24:19 GMT',
server: 'UploadServer',
'alt-svc': 'hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="41,39,35"' },
rawHeaders:
[ 'X-GUploader-UploadID',
'AEnB2UqADzuHb4O7UjdBAui1cDWeLNO4s0YuT2krCPoYIHaUrYPjXRH8rBU0mcSi9n7sie11PhALTN2vOKkhykW0apqTbrNB9Q',
'Vary',
'Origin',
'Vary',
'X-Origin',
'Content-Type',
'text/html; charset=UTF-8',
'Content-Length',
'9',
'Date',
'Sat, 17 Mar 2018 13:24:19 GMT',
'Server',
'UploadServer',
'Alt-Svc',
'hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="41,39,35"' ],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 404,
statusMessage: 'Not Found',
client:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 9,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'www.googleapis.com',
_readableState: [Object],
readable: true,
domain: [Object],
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 4047,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: null,
read: [Function],
_consuming: true,
_idleTimeout: -1,
_idleNext: null,
_idlePrev: null,
_idleStart: 1307656 },
_consuming: true,
_dumped: false,
req:
ClientRequest {
domain: [Object],
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: false,
upgrading: false,
chunkedEncoding: true,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /upload/storage/v1/b/myprojectid.appspot.com//o?uploadType=multipart&name=profile HTTP/1.1\r\nUser-Agent: gcloud-node-storage/1.4.0\r\nx-goog-api-client: gl-node/6.11.5 gccl/1.4.0\r\nAuthorization: Bearer ya29.c.<PLACEHOLDER_FOR_SOME_LONG_AUTH_ID>\r\nhost: www.googleapis.com\r\naccept-encoding: gzip, deflate\r\ntransfer-encoding: chunked\r\ncontent-type: multipart/related; boundary=6e012a90-2cb1-4c46-9de5-bb75cb5949ea\r\nConnection: keep-alive\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/upload/storage/v1/b/myprojectid.appspot.com//o?uploadType=multipart&name=profile',
_ended: true,
parser: null,
timeoutCb: null,
res: [Circular] },
request:
Request {
domain: [Object],
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
timeout: 60000,
gzip: true,
forever: true,
pool: [Object],
method: 'POST',
uri: [Object],
headers: [Object],
callback: [Function],
readable: true,
writable: true,
explicitMethod: true,
_qs: [Object],
_auth: [Object],
_oauth: [Object],
_multipart: [Object],
_redirect: [Object],
_tunnel: [Object],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
dests: [],
__isRequestRequest: true,
_callback: [Function: onResponse],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.googleapis.com',
url: [Object],
path: '/upload/storage/v1/b/myprojectid.appspot.com//o?uploadType=multipart&name=profile',
httpModule: [Object],
agentClass: [Object],
agentOptions: [Object],
agent: [Object],
src: [Object],
_started: true,
href: 'https://www.googleapis.com/upload/storage/v1/b/myprojectid.appspot.com//o?uploadType=multipart&name=profile',
req: [Object],
ntick: true,
response: [Circular],
originalHost: 'www.googleapis.com',
originalHostHeaderName: 'host',
responseContent: [Circular],
_destdata: true,
_ended: true,
_callbackCalled: true },
toJSON: [Function: responseToJSON],
caseless: Caseless { dict: [Object] },
read: [Function],
body: 'Not Found' },
message: 'Not Found' }
Unfortunately the 'upload' function only receive path to local file or url, so it cause some complexity when implement uploading file REST api by cloud functions.
I'm guessing it shouldn't be a proper way to upload a file to cloud function server and upload a local file to cloud storage.
Here's what I accomplished to make upload endpoint to firebase (gcloud) storage using multer to get file from the request, then convert it to data and use save function to upload.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const multer = require('multer');
const app = express();
const fileUpload = multer();
admin.initializeApp(.......);
/*
* Upload File
**/
app.post('/', fileUpload.single('file'), functions.https.onRequest((req, res) => {
const file = req.file;
const bucket = admin.storage().bucket();
const name = file.originalname;
const bucketFile = bucket.file(name);
bucketFile
.save(new Buffer(file.buffer))
.then(() => {
res.status(200).json({
status: 'success',
data: Object.assign({}, bucketFile.metadata, {
downloadURL: `https://storage.googleapis.com/${bucket.name}/${name}`,
})
});
})
.catch(err => {
res.status(500).json({
status: 'error',
errors: err,
});
});
}));
Here's my POSTMAN screen for test.
Let me know if you need further help.

ECONNREFUSED error while creating database ArangoDB with Nodejs

I am following this tutorial from ArangoDB.com.
It is about creating Arango database using node.js. I am doing it on OS X.
After executing in node code:
> Database = require('arangojs').Database;
[Function: Database]
> db = new Database('http://127.0.0.1:8529');
Database {
_connection:
Connection {
config:
{ url: 'http://127.0.0.1:8529',
databaseName: '_system',
arangoVersion: 20300,
agentOptions: [Object],
headers: [Object] },
_request: [Function: request],
promisify: [Function] },
_api:
Route {
_connection:
Connection {
config: [Object],
_request: [Function: request],
promisify: [Function] },
_path: '_api',
_headers: undefined },
name: '_system' }
> db.createDatabase('mydb', function (err) {
if (!err) console.log('Database created');
else console.error('Failed to create database:', err);
});
I am getting ECONNREFUSED error:
Failed to create database: { [Error: connect ECONNREFUSED 127.0.0.1:8529]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8529,
request:
ClientRequest {
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { response: [Object], error: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: { 'content-length': false },
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
Socket {
_connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: null,
_readableState: [Object],
readable: false,
domain: [Object],
_events: [Object],
_eventsCount: 10,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
bytesRead: 0,
_bytesDispatched: 0,
_sockname: null,
_pendingData: [Object],
_pendingEncoding: '',
server: null,
_server: null,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
connection:
Socket {
_connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: null,
_readableState: [Object],
readable: false,
domain: [Object],
_events: [Object],
_eventsCount: 10,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
bytesRead: 0,
_bytesDispatched: 0,
_sockname: null,
_pendingData: [Object],
_pendingEncoding: '',
server: null,
_server: null,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
_header: 'POST /_db/_system/_api/database HTTP/1.1\r\ncontent-type: application/json\r\ncontent-length: 15\r\nx-arango-version: 20300\r\nHost: 127.0.0.1:8529\r\nConnection: keep-alive\r\n\r\n',
_headers:
{ 'content-type': 'application/json',
'content-length': 15,
'x-arango-version': 20300,
host: '127.0.0.1:8529' },
_headerNames:
{ 'content-type': 'content-type',
'content-length': 'content-length',
'x-arango-version': 'x-arango-version',
host: 'Host' },
_onPendingData: null,
agent:
Agent {
domain: [Object],
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: true,
maxSockets: 3,
maxFreeSockets: 256 },
socketPath: undefined,
method: 'POST',
path: '/_db/_system/_api/database',
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Object],
incoming: null,
outgoing: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] } } }
Unfortunately I can't localise error. I was searching for similar porblems but didn't find any solutions. I don't have much experience with these technologies and I am just starting ArangoDB now.
I would be grateful for any tips how to solve it and/or any other materials how to start with ArangoDB.
The error indicates that the client could not connect, presumably ArangoDB is not running at port 8529 on localhost, hasn't been started or hasn't finished starting.
The tutorial should work from the node shell but you may have to avoid line breaks (e.g. before a . when calling a method like .then). The code in the examples has been formatted for readability, the screenshots may be safer if you want something to follow along.
In general if you have multiple line of code that you need to run using node it is best to create a JavaScript file that contains all the code you want to run such as script.js. To run that code you use node script.js. This will cause node to evaluate your code line by line and (potentially) persistently keep the code running the code (things like a server would do this).
Using the node shell (aka just running node directly) will not be a good strategy to have persistently running code.
So in conclusion: place any server code in a script.js then run it with the command node script.js inside of a bash shell (where node is installed).
It very likely code not start a server and persistently consume a port while inside the shell.

Resources