I would like to get the file from this url : https://donnees.roulez-eco.fr/opendata/instantane
As you can see there's no file name in the URL.
My problem is with this simple code :
var file = fs.createWriteStream('./myFile.zip')
http.get(url, function(response) {
response.pipe(file);
}
This code works well if you got an url like https://someurl.com/filename.zip.
The file is downloaded and can be use but this response.pipe(file) line throw me this error:
events.js:163
throw er; // Unhandled 'error' event
^
Error: Parse Error
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:559:20)
What should / can i do to fix this?
Related
I am using the HTTP module of nodejs to create three routes('/','/about' and the last one treats any other route that is not defined as error route). When I access the root route first and try to access other route nodejs throw an error but when I access the error route or the about the route and try accessing another route it works fine.
Below are the code I wrote and the error nodejs throw
Error
PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
Code
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome Home Dev, You are loved');
}
if(req.url==='/about'){
res.end('This is the about page')
}
res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>
<a href='/'>back to homepage</a>
`
);
});
server.listen(5000);
Change to an if/else so you're only processing one branch of the if per request:
const server = http.createServer((req,res)=>{
if(req.url === '/'){
res.end('Welcome Home Dev, You are loved');
} else if (req.url === '/about') {
res.end('This is the about page')
} else {
res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>
<a href='/'>back to homepage</a>`);
}
});
Or, alternately, you could add a return after each res.send() to stop further execution in your request handler after you send a response. Remember, that just because you call res.send() your function still continues to execute so you need to manage control flow so the other code that sends a response doesn't execute once you've already sent a response.
server is crashing automatically and showing this error. can you guys please me to get out from this problem>>>>
Warning: got packets out of order. Expected 3 but received 0
node:events:368
throw er; // Unhandled 'error' event
^
Error: The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
at Connection.protocolError (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:394:17)
at Connection.handlePacket (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:445:14)
at PacketParser.onPacket (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:85:12)
at PacketParser.executeStart (/home/abroadinquiry/server/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket. (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:92:25)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
Emitted 'error' event on Connection instance at:
at Connection.protocolError (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:397:10)
at Connection.handlePacket (/home/abroadinquiry/server/node_modules/mysql2/lib/connection.js:445:14)
[... lines matching original stack trace ...]
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
fatal: true,
code: 4031
I'm tryng to send a Curl request using "request-promise" of npm. The Curl that I want to send is as follows:
`curl \
-H "Content-Type: multipart/form-data" \
-F "original=#./${parent_path}" \
-F "modified=#./${version_path}" \
-o "${out_path}" \
${URI}`
My code in node is:
BIMFile.findOne({ _id: responseDB.parent_id })
.then(parent => {
parent_path = parsePath(parent.path);
version_path = parsePath(responseDB.path);
console.log("PARENT!", parent_path, version_path);
const URI =
`${protocol}://${host_img_diff}:${port_img_diff}/diff`
out_path = version_path + '.tmp.jpg';
request.post({
url: URI,
formData: {
file: fs.createReadStream(parent_path),
file: fs.createReadStream(version_path)
}
}).then((apiResponse) => {
console.log('apiUPDATEResponse', apiResponse);
})
The result is:
Unhandled rejection StatusCodeError: 400 - "<!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad
Request</title>\n<h1>Bad Request</h1>\n<p>The browser (or proxy) sent a re
quest that this server could not understand.</p>\n"
at new StatusCodeError (/backend/node_modules/request-promise-
core/lib/errors.js:32:15)
at Request.plumbing.callback (/backend/node_modules/request-promise-
core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/backend/node_modules/request-
promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/backend/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/backend/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous>
(/backend/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
The server returns the following message:
xxx.xx.x.xx - - [05/Apr/2019 10:00:15] "POST /diff HTTP/1.1" 400 -
As you can see the server couldn't understand the post request. Does anyone know how to add files correctly?
Use send files using formData as you're doing, but one of the issues you have is that you're setting both files in the same property, and only the last one is set.
console.log({ file: 1, file: 2 });
So if file can receive multiple files, you need to use an array
const formData = {
file: [
fs.createReadStream(parent_path),
fs.createReadStream(version_path)
]
}
If you need additional meta-data, request module provides a way too:
Pass optional meta-data with an 'options' object with style:
{value: DATA, options: OPTIONS}
Use case: for some types of
streams, you'll need to provide "file"-related information manually.
See the form-data README for more information about options:
https://github.com/form-data/form-data
I'm getting the following error when trying to run the following code. My goal is to delete all items in both the User and Item tables. User and Item correspond to Dynamoose models.
The for loop is running correctly. But the scan().exec callback function isn't being run at all and it's throwing the following error.
What is really strange is on CircleCI this problem doesn't happen. It only happens on my local computer. I have tried removing node_modules and running npm install with no luck. Also it works fine if I remove the code below.
Any ideas? Or even where to start debugging this?
Code:
var dbarray = [User, Item];
for (var i = 0; i < dbarray.length; i++) {
dbarray[i].scan().exec(function(err, items) {
if (err) {
throw err;
}
items.forEach(function(item, key) {
item.delete();
});
});
}
Error:
General
1) "before each" hook
0 passing (10s)
1 failing
1) "before each" hook:
Uncaught NetworkingError: Cannot read property 'replace' of undefined
at findTargetPort (node_modules/zombie/lib/reroute.js:50:28)
at Socket.Net.Socket.connect (node_modules/zombie/lib/reroute.js:69:18)
at Agent.connect [as createConnection] (net.js:106:35)
at Agent.createSocket (_http_agent.js:217:26)
at Agent.addRequest (_http_agent.js:187:10)
at new ClientRequest (_http_client.js:272:16)
at Object.request (http.js:39:10)
at features.constructor.handleRequest (node_modules/aws-sdk/lib/http/node.js:42:23)
at executeSend (node_modules/aws-sdk/lib/event_listeners.js:304:29)
at Request.SEND (node_modules/aws-sdk/lib/event_listeners.js:318:9)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at node_modules/aws-sdk/lib/event_listeners.js:220:9
at finish (node_modules/aws-sdk/lib/config.js:315:7)
at node_modules/aws-sdk/lib/config.js:333:9
at Credentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:327:24)
at Config.getCredentials (node_modules/aws-sdk/lib/config.js:347:9)
at Request.SIGN (node_modules/aws-sdk/lib/event_listeners.js:192:22)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Timeout.callNextListener [as _onTimeout] (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
The issue turned out to be with Zombie.js. Specially Browser.localhost('localhost', port);. Removing that line and passing in http://localhost:3000 before each visit command solved the problem.
Not sure why the error was mentioning AWS and things like that.
getJSON('https://api.twitch.tv/kraken/streams/Jonathan_x64',
function(channel) {
if (channel["stream"] == null) {
//do something
} else {
////do something else
}
});
that is my current code but when i run it i get the following error
if (channel["stream"] == null) {
^
TypeError: Cannot read property 'stream' of undefined
at E:\my ultemet bot\index.js:10:16
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
at Request.self.callback (E:\my ultemet bot\node_modules\request\request.js:200:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:1067:10)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:988:12)
at emitNone (events.js:91:20)
As far as I know there isn't a built-in top level getJSON() function in Node so you must be using a custom function.
From the stack trace you've shared:
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
^^^^^^^^^^^^^^^^^^^^^
... we learn that you are using an NPM third-party module. Once there, it's trivial to find the documentation:
var getJSON = require('get-json')
getJSON('http://api.listenparadise.org', function(error, response){
error
// undefined
response.result
// ["Beth Orton — Stolen Car",
// "Jack White — Temporary Ground",
// "I Am Kloot — Loch",
// "Portishead — Glory Box"]
response.ok
// => true
})
It isn't real code but it's clear that the first callback argument is error, but you have this:
function(channel){}
Since (as the error message states) it's undefined, that means that the call is successful—it's just you aren't reading it correctly.
I've been peeking the module source code and it's actually not very impressive. It's basically a tiny wrapper for request that doesn't add much value.