Node (express.js) next() is called before end of stream - javascript

I have the following middleware function
var bodyParser = require('body-parser'),
fs = require('fs');
module.exports = function(req, res, next) {
// Add paths to this array to allow binary uploads
var pathsAllowingBinaryBody = [
'/api2/information/upload',
'/api2/kpi/upload',
];
if (pathsAllowingBinaryBody.indexOf(req._parsedUrl.pathname) !== -1) {
var date = new Date();
req.filePath = "uploads/" + date.getTime() + "_" + date.getMilliseconds() + "_" + Math.floor(Math.random() * 1000000000) + "_" + parseInt(req.headers['content-length']);
var writeStream = fs.createWriteStream(req.filePath);
req.on('data', function(chunk) {
writeStream.write(chunk);
});
req.on('end', function() {
writeStream.end();
next();
});
} else {
bodyParser.json()(req, res, next);
}
};
The files is being transfered correctly however sadly the next() in the
req.on('end', function() {
writeStream.end();
next();
});
is called before it is done writing all data to the new file.
My question is what am i doing wrong? And how can i fix it?

Use the writable file stream's close event to know when the file descriptor has been closed.
Replace this:
var writeStream = fs.createWriteStream(req.filePath);
req.on('data', function(chunk) {
writeStream.write(chunk);
});
req.on('end', function() {
writeStream.end();
next();
});
with this:
req.pipe(fs.createWriteStream(req.filePath)).on('close', next);

Related

JQuery ajax call returns 404 page not found

I am trying to parse a JSON string upon loading the page but I get the following error in the web dev tools: GET http://ipaddress/CulturalEvents/calWrapper 404 not found (Note: ipaddress is the address for our IIS web server). When I click on the error I get the following error: Failed to load resource: the server responded with a status of 404 not found.
Here is my index.js
var titles = new Array();
var descriptions = new Array();
var count = 0;
// Function to cycle through events on display
function changeText() {
$('#evtName').html(titles[count]);
$('#evtDesc').html(descriptions[count]);
if (count < titles.length - 1) {
count++;
} else {
count = 0;
}
}
$(document).ready(function () {
$.ajax({
url:'/CulturalEvents/calWrapper',
type: 'GET',
dataType: 'json',
success: function(calJSON){
let eventCheck = 0;
var today = new Date();
var yyyy = today.getFullYear();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
today = yyyy + mm + dd;
console.log(today);
for (let i = 0; i < calJSON.length; i++){
if (calJSON[i].startDT == today){
eventCheck = 1;
} else {
eventCheck = 0;
}
if (eventCheck == 1){
titles.push(calJSON[i].summary);
if (calJSON[i].description == ""){
descriptions.push("No description.");
} else{
descriptions.push(calJSON[i].description)
}
} else {
titles.push("No events today.");
descriptions.push("If you know of an event that is not displayed feel free to contact the Diversity Equity and Inclusion committee.")
}
}
}
});
// Rotate through events
changeText();
setInterval(changeText, 10000);
});
It can't find my ajax url '/CulturalEvents/calWrapper'. Note I can run this locally and look for the endpoint /calWrapper and it works perfectly fine, but when I run it on the IIS server it stops working.
Here is my app.js as well:
// C library API
const ffi = require('ffi');
// Express app
const express = require('express');
const app = express();
const path = require('path')
const port = process.env.PORT
const fs = require('fs');
app.use(express.static('public'));
// Send HTML
app.get('/CulturalEvents/', function(req, res){
res.sendFile(path.join(__dirname + '/public/index.html'));
});
// Send style
app.get('/CulturalEvents/style.css', function(req, res) {
res.sendFile(path.join(__dirname + '/public/style.css'));
});
// send JavaScript
app.get('/CulturalEvents/index.js', function (req, res) {
res.readFile(path.join(__dirname + '/public/index.js'), 'utf8', function(err, contents){
res.send(contents);
});
});
// Wrapper function for c library
let wrapper = ffi.Library('./bin/libcalWrapper', {
'calWrapper': [ 'string', [ 'string' ] ]
});
app.get('/CulturalEvents/calWrapper', function (req, res) {
var tempStr = JSON.parse(wrapper.calWrapper(__dirname + "/multiculturalcalendar2021.ics"));
res.send(tempStr);
});
app.listen(port, () => {
console.log(__dirname + '/public/index.js');
});
Also the directory structure is as follows:
CulturalEvents/
public/
index.js
index.html
style.css
app.js
package.json
web.confi

Download a file from web using Node js and loop

I want to download multiple files from the web using this code:
var fs = require('fs');
var http = require('http');
var request = require('request');
var file;
for(var i = 1; i <= 5; i++) {
//CHECK IF REMOTE FILE EXISTS
request('http://webaddress.com/filename' + i + '.jar', function (err, resp) {
//IF EXISTS DO
if (resp.statusCode == 200) {
//DOWNLOAD DATA AND CREATE A NEW .JAR FILE
file = fs.createWriteStream('D:\\filename' + i + '.jar');
http.get('http://webaddress.com/filename' + i + '.jar', function(response) {
response.pipe(file);
file.on('finish', function() {
file.close();
});
});
}
//FILE DOES NOT EXIST
});
}
The result I want is: multiple files downloaded with filenames filename1-5.jar. The result I am getting is just 1 file with filename filename5.jar (or the last value of the i var in the loop). What am I doing wrong?
Like #Ionut said your requests are async so you need to wait for it
let fs = require('fs');
let request = require('request');
let download = (uri, filename) => {
return new Promise ((resolve, reject) => {
request.head(uri, function(err, res) {
if (res.statusCode === 200) {
request(uri).pipe(fs.createWriteStream(filename)).on('close', resolve);
} else {
reject(res.statusCode);
}
});
});
};
let promises = [];
for(let i = 1; i <= 5; i++) {
promises.push(download('http://webaddress.com/filename' + i + '.jar', 'D:\\filename' + i + '.jar'));
}
Promise.all(promises).then(() => {
process.exit(0);
});
Your request is asynchronous and it will execute only after your loop finishes hence the 5 from the filename. A solution for this is to threat your code separately by creating a new function and call it inside the loop:
var fs = require('fs');
var http = require('http');
var request = require('request');
var file;
function customRequest(i){
//CHECK IF REMOTE FILE EXISTS
return request('http://webaddress.com/filename' + i + '.jar', function(err, resp) {
//IF EXISTS DO
if (resp.statusCode == 200) {
//DOWNLOAD DATA AND CREATE A NEW .JAR FILE
file = fs.createWriteStream('D:\\filename' + i + '.jar');
http.get('http://webaddress.com/filename' + i + '.jar', function(response) {
response.pipe(file);
file.on('finish', function() {
file.close();
});
});
}
//FILE DOES NOT EXIST
});
}
for (var i = 1; i <= 5; i++) {
customRequest(i)
}

Write data to JSON file

I'm trying to write a list of habit objects to a JSON file, but I can't figure it out. I'm doing this because I want to use this JSON file with node.js.
I've created a section with a class called "habits" that I want to fill this JSON file with, the section itself gets the habit objects from this piece of code:
var addHabit = function () {
var $new_habit = {
"name":"",
"value":0,
"goal":0,
"html": null
}
if ($(".habit-input input").val() !== "") {
$new_habit.name = $("#habitinput .name").val();
$new_habit.value = $("#habitinput .value").val();
$new_habit.goal = $("#habitinput .goal").val();
$new_habit.html = $("<p id="+uid+">").html("<span class=\"name\">"+$new_habit.name+"</span> <span class=\"val\">" + $new_habit.value + "</span>/" + "<span class=\"goal\">"+$new_habit.goal + "</span>"+
"<button class=\"plus\">+</button><button class=\"min\">-</button><button class=\"delete\">x</button><button class=\"mod\">modify</button>");
console.log($new_habit);
$(".habits").append($new_habit.html);
$(".habit-input input").val("");
$("#"+uid + " .plus").on("click", function (event) {
var val = parseInt( $(this).parent().find(".val").text());
$(this).parent().find(".val").text( val+1 );
event.preventDefault();
});
$("#"+uid + " .min").on("click", function (event) {
var val = parseInt( $(this).parent().find(".val").text());
$(this).parent().find(".val").text( val-1 );
event.preventDefault();
});
$("#"+uid + " .delete").on("click", function (event) {
$(this).parent().remove();
event.preventDefault();
});
$("#"+uid + " .mod").on("click", function (event) {
var val = parseInt( $(this).parent().find(".val").text());
var name = parseInt( $(this).parent().find(".name").text());
var goal = parseInt( $(this).parent().find(".goal").text());
$(this).parent().find(".val").text(val = $("#habitinput .value").val());
$(this).parent().find(".name").text(name = $("#habitinput .name").val());
$(this).parent().find(".goal").text(goal = $("#habitinput .goal").val());
event.preventDefault();
});
uid++;
};
};
I want to read the section "habits" into the JSON file so I can use it in this node.js file:
var express = require("express");
var url = require("url");
var http = require("http");
var fs = require('fs');
var port = 3000;
var app = express();
app.use(express.static(__dirname + "/client"));
//logger component
app.use(function (req, res, next) {
console.log("[LOG] %s %s", new Date(), req.url);
next();
});
http.createServer(app).listen(port);
//clients requests habits
app.get("/habits", function (req, res) {
console.log("Habits requested!");
res.sendfile('Habit/client/habits.json');
});
What I hope to achieve is that when I go to localhost:3000/habits in my browser that I get all the habit objects in JSON format.
At the moment when I go to localhost:3000/habits I get this {"type":"Buffer","data":[123,13,10,32,32,32,32,34,121,101,121,34,32,58,32,49,50,13,10,125]}
Any help would be greatly appreciated
You're getting a buffer back from the GET/habits. Need to convert it to a something you can read
try calling toString('utf-8')
OR
import bodyParser from 'body-parser';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

Can not read property '/' of undefined

I started studying node.js. I ask you questions while studying. when I run my code(server) and connect to localhost, It doesn't work properly.
This is error:
This is my code:
index.js
var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');
var handle = {};
handle['/'] = requestHandlers.view;
handle['/view'] = requestHandlers.view;
handle['/create'] = requestHandlers.create;
server.start(router.route, requestHandlers.handle);
server.js
var http = require('http');
var url = require('url');
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log('\nrequest for ' + pathname + ' received.');
response.writeHead(200, {'Content-Type' : 'text/plain'});
// route(handle, pathname); // injected function call
var content = route(handle, pathname);
response.write(content);
response.end();
}
http.createServer(onRequest).listen(8000);
console.log('server has started.');
}
exports.start = start;
router.js
function route(handle, pathname) {
console.log('about to route a request for ' + pathname);
if (typeof handle[pathname] === 'function') {
return handle[pathname]();
} else {
console.log('no request handler found for ' + pathname);
return "404 Not found";
}
}
exports.route = route;
requestHandlers.js
function view(response) {
console.log('request handler called --> view');
return "Hello View";
}
function create(response) {
console.log('request handler called --> create');
return "Hello Create";
}
exports.view = view;
exports.create = create;
In index.js, you're passing requestHandlers.handle, which doesn't exist, rather than the handle object that you've created.
var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');
var handle = {};
handle['/'] = requestHandlers.view;
handle['/view'] = requestHandlers.view;
handle['/create'] = requestHandlers.create;
// server.start(router.route, requestHandlers.handle);
server.start(router.route, handle);

save mp3 as ajax post data with node js

Not sure what am I doing wrong...
I have a node js webserver that should save a mp3 file to disk from an ajax post. The post contains an mp3 file constructed from a wav file created by recorder.js in the browser.
In my requestHandler I have the following code:
var
formidable = require('formidable'),
http = require('http'),
path = require('path'),
fs = require('fs');
function requestHandler(req, res) {
var requestPath = path.basename(req.url) || 'index.html',
ext = path.extname(requestPath),
localFolder = __dirname + '/public/',
uploadFolder = __dirname + '/uploads/';
if(requestPath === 'uploadmp3' && req.method === 'POST') {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if(err) {
console.error(err.message);
return;
}
var mp3File = uploadFolder + fields.fname;
var base64File = new Buffer(fields.data, 'binary').toString('base64');
var decodedFile = new Buffer(base64File, 'base64').toString('binary');
fs.writeFileSync(mp3File, decodedFile, 'base64', function(err){
if(err) {
return console.log(err);
}
console.log('written to disk: ' + mp3File);
});
res.writeHead(200, {'Content-Type:' : 'text/plain'});
res.write(mp3File+ ' \n\n');
res.end('\n');
});
return;
}
}
The result is that I save the file to disk, but although it has the correct size, the time is not set properly and the playback last for one second.
I can save it with php with no problem... mp3 file plays perfectly, but I really need this to work on node js.
You need to use Stream to save contents to disk. As per your implementation:
var fs = require('fs');
if(requestPath === 'uploadmp3' && req.method === 'POST') {
var mp3File = uploadFolder+'audio_feedback_' + new Date().getTime() + '.mp3';
var mp3_file = fs.createWriteStream(mp3File);
mp3_file.on('open', function (fd) {
req.on('data', function(data){
console.log("loading... \n");
mp3_file.write(data);
});
req.on('end', function(){
console.log("finalizing...");
mp3_file.end();
res.writeHead(200, {'Content-Type:' : 'text/plain'});
res.write(mp3File+ ' is written to disk');
res.end('\n');
});
});
}
Hope this helps.
I have fixed it with:
if(requestPath === 'uploadmp3' && req.method === 'POST') {
var mp3 = '';
var mp3File = uploadFolder+'audio_feedback_' + new Date().getTime() + '.mp3';
// req.setEcoding('base64');
req.on('data', function(data){
console.log("loading... \n");
mp3 += data;
});
req.on('end', function(){
console.log("request completed");
fs.open(mp3File, 'w', function(err, fd) {
if(err) {
return console.log(err);
}
// console.log(mp3File + ' file was read...');
fs.writeFile(mp3File, mp3, 'base64', function(err){
if(err) {
return console.log(err);
}
console.log('written to disk: ' + mp3File);
});
});
res.writeHead(200, {'Content-Type:' : 'text/plain'});
res.write(mp3File+ ' is written to disk');
res.end('\n');
});
}

Categories