I am building file management system using pure node.js and html.
app.js and requestHandler.js seems to be working fine for other things like get/post request with parameters.
I can accept the parameters in requestHandler.js using parameters object. For example, if I want to access username, i can access using parameters.username.
For file upload functionality, I did following in JS.
Problem with following code is, file is getting damaged while saving for image files. but if i upload txt file, that is getting saved properly.
What i am doing wrong for image file?
$("#upload-file").on("click", function(){
var file = document.getElementById('file'); //Files[0] = 1st file
file = file.files[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = function(event){
var result = event.target.result;
var fileName = document.getElementById('file').files[0].name; //Should be 'picture.jpg'
$.post('/api/upload', { data: result, name: fileName }, function(){
});
};
//reader.onloadstart = ...
//reader.onprogress = ... <-- Allows you to update a progress bar.
//reader.onabort = ...
//reader.onerror = ...
//reader.onloadend = ...
});
app.js
var http = require('http');
var fs = require('fs');
var path = require('path');
var qs = require('querystring');
var api = require('./requestHandler.js');
var server = http.createServer(function (req, res) {
if(req.url != "/") {
var requestUrl = req.url;
if(requestUrl.indexOf("api") != -1) {
var element = {};
var apiClass = requestUrl.split("/");
var apiClass = apiClass[2].split("?");
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
if (body.length > 1e6) {
req.connection.destroy();
}
});
req.on('end', function () {
var element = qs.parse(body);
api.handle(apiClass[0], element, res);
});
} else {
var query = req.url;
query = query.split("?");
if(query.length == 2) {
query = query[1].split("&");
query.forEach(function(item, index){
item = item.split("=");
element[item[0]] = item[1];
});
}
api.handle(apiClass[0], element, res);
}
} else {
fs.readFile(__dirname + requestUrl, function (err, data) {
switch(path.extname(requestUrl)) {
case ".css":
res.writeHead(200, { 'Content-Type': 'text/css' });
break;
case ".js":
res.writeHead(200, { 'Content-Type': 'text/javascript' });
break;
case ".woff":
res.writeHead(200, { 'Content-Type': 'application/font-woff' });
break;
case ".ttf":
res.writeHead(200, { 'Content-Type': 'application/x-font-ttf' });
break;
case ".eot":
res.writeHead(200, { 'Content-Type': 'application/vnd.ms-fontobject' });
break;
case ".svg":
res.writeHead(200, { 'Content-Type': 'application/svg+xml' });
break;
}
res.end(data, 'utf-8');
res.end();
});
}
} else {
fs.readFile('index.html', function (err, data) {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
});
}
});
server.listen(888);
console.log("server listening on 888");
requestHandler.js
var fs = require('fs');
module.exports = {
handle: function (requestURL, parameters, res) {
switch(requestURL) {
case "upload":
fs.writeFile("./files/" + parameters.name, parameters.data, 'binary', function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
break;
}
}
};
Related
I am trying to make a server for a website. The server works perfectly, and it can receive and send data normally. I have hosted the server, and I made a demo client-side repl on Replit to test the flow. But somehow, either the reply isn't receiving or sending properly, or the server isn't working. (For the client-side code, I am using JQuery)
// Client Side Code
const url = "My url";
const data = {
"file": "lol.html"
}
function start() {
console.log("test 1")
$.post(url, data, function(data, status) {
console.log(data + "is data & status is " + status);
});
}
// Server-side code
var http = require('http'); // Import Node.js core module
var fs = require('fs'); // File System
var url = require('url');
var server = http.createServer(function(req, res) { //create web server
if (req.url == '/') { //check the URL of the current request
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ message: "This is the Backend server for my website" }));
res.end();
}
else if (req.url == "/ping") {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ message: "Pong!" }));
res.end();
}
else if (req.url.startsWith("/read")) {
var q = url.parse(req.url, true);
var qdata = q.query;
fs.readFile(String(qdata.file), function(err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end("404 Not Found");
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
return res.end();
});
}
else if (req.url.startsWith("/get")) {
var q = url.parse(req.url, true);
var qdata = q.query;
fs.readFile(String(qdata.file), function(err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end("404 Not Found");
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
return res.end();
});
}
else {
res.end('Invalid Request!');
}
});
server.listen(5000); //6 - listen for any incoming requests
console.log('Node.js web server at port 5000 is running..')
Can Someone tell me how to perform this properly?
I am trying to request data from RESTful API and sharing the result to html web page using Node.js. My code runs well, but I want to make this RESTful request every time I call the webpage not just when I run a Node.js server.
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var request = require("request");
var temp = "";
var options = { method: 'GET',
url: 'My_URL',
headers: { authorization: 'Basic My_Autho' }
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
temp = body;
console.log(body);
});
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'
});
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var renderedHtml = ejs.render(content, {temp: temp});
res.end(renderedHtml);
});
}).listen(8000);
Maybe you could move your call to the external REST api inside the request handler ?
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var request = require("request");
var temp = "";
var options = { method: 'GET',
url: 'My_URL',
headers: { authorization: 'Basic My_Autho' }
};
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
request(options, function (error, response, body) {
if (error) throw new Error(error);
temp = body;
console.log(body);
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var renderedHtml = ejs.render(content, {temp: temp});
res.end(renderedHtml);
});
});
}).listen(8000);
New to node and trying not to do any callback hell.
I have two files
routes.js
fetch.js
//routes.js
var fetchController = require("../lib/mtl_fetcher/fetcher_controller");
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send(fetchController.getAllTransgressor(function(results) {
return results.end();
}))
});
module.exports = router;
and
//fetch.js
var http = require('http');
var config = require('./config')
var Iconv = require('iconv').Iconv
module.exports.getAllTransgressor = function(callback) {
var req = http.get(config.urlOptions.host, function (response) {
var bufferChunk = [];
var str
if(response.statusCode == 200) {
response.on('data', function(chunk) {
bufferChunk.push(chunk);
})
response.on('end', function(callback) {
var iconv = Iconv('latin1', 'UTF-8');
str = iconv.convert(Buffer.concat(bufferChunk)).toString();
console.log(str)
});
} else {
console.log("handle this")
}
});
req.on("error", function(err) {
callback(err);
});
callback(req)
}
So the goal is to fetch and then show what has been fetch to the screen. The ressource is XML base.
Doing all of this in one block (routes.js) works, but when I try to refactor and set up some modularity my str just shows in shell stdout.
Using req.end() does not send the content back.
Firstly, you need to send inside the callback, where the result is actually available, as you can't return from an asynchronous function
router.get('/', function(req, res, next) {
fetchController.getAllTransgressor(function(error, results) {
if ( error ) {
// handle errors
} else {
res.send(results);
}
});
});
The same goes for the callback function, it has to be called when the data is available, after the request and parsing
module.exports.getAllTransgressor = function(callback) {
var req = http.get(config.urlOptions.host, function(response) {
var bufferChunk = [];
if (response.statusCode == 200) {
response.on('data', function(chunk) {
bufferChunk.push(chunk);
});
response.on('end', function() {
var iconv = Iconv('latin1', 'UTF-8');
var str = iconv.convert(Buffer.concat(bufferChunk)).toString();
callback(null, str); // here, stuff is available
});
} else {
callback('Did not return 200', err);
}
});
req.on("error", function(err) {
callback(err, null);
});
}
I am trying to download a PDF file using NodeJS then send its data to client to be embedded in the page. Here is how I download the PDF file:
exports.sendPdf = function(req, responce) {
var donneRecu = req.body;
var url = 'http://www.ieee.org/documents/ieeecopyrightform.pdf'//pdf link
http.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
console.log('downloading');
data += chunk;
});
res.on("end", function() {
console.log('downloaded');
responce.header("Access-Control-Allow-Origin", "*");
responce.header("Access-Control-Allow-Headers", "X-Requested-With");
responce.header(200, {'content-type' : 'application/pdf'});
responce.send(data);
});
}).on("error", function() {
callback(null);
});
}
How do I send the data received from NodeJS to the client side?
EDIT
i found the solution :
exports.sendPdf = function(req, res) {
var donneRecu = req.body;
console.log(donneRecu['lien']);
var url = donneRecu['lien']; //pdf link
http.get(url, function(response) {
var chunks = [];
response.on('data', function(chunk) {
console.log('downloading');
chunks.push(chunk);
});
response.on("end", function() {
console.log('downloaded');
var jsfile = new Buffer.concat(chunks).toString('base64');
console.log('converted to base64');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('content-type', 'application/pdf');
res.send(jsfile);
});
}).on("error", function() {
callback(null);
});
}
next in my angular controller:
var pdf = $scope.base64ToUint8Array(data);
PDFJS.getDocument(pdf).then(functiongetPdfHelloWorld(_pdfDoc) {
$scope.pdfDoc = _pdfDoc;$scope.renderPage($scope.pageNum); });
i found the solution :
exports.sendPdf = function(req, res) {
var donneRecu = req.body;
console.log(donneRecu['lien']);
var url = donneRecu['lien']; //pdf link
http.get(url, function(response) {
var chunks = [];
response.on('data', function(chunk) {
console.log('downloading');
chunks.push(chunk);
});
response.on("end", function() {
console.log('downloaded');
var jsfile = new Buffer.concat(chunks).toString('base64');
console.log('converted to base64');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('content-type', 'application/pdf');
res.send(jsfile);
});
}).on("error", function() {
callback(null);
});
}
next in my angular controller:
var pdf = $scope.base64ToUint8Array(data);
PDFJS.getDocument(pdf).then(functiongetPdfHelloWorld(_pdfDoc) {
$scope.pdfDoc = _pdfDoc;$scope.renderPage($scope.pageNum); });
I too faced the same situation and the below code helped me.
var fs = require("fs");
var file = fs.createReadStream('./public/modules/datacollectors/output.pdf');
var stat = fs.statSync('./public/modules/datacollectors/output.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=quote.pdf');
file.pipe(res);
Hope it helps.
So I have a Node.js script and a Javascript file communicating with each other, and everything works except the Node.js is supposed to return data for an .mp3 file.
The data is binary, it looks like gibberish, how would I take that data it returns and allow the user to download it on a webpage using Javascript?
It gets data using http.responseText by the way.
Node.js Code
//initilization
var querystring = require('querystring');
var http = require('http');
var url = require('url');
var fileSystem = require('fs');
var path = require('path');
var util = require('util');
//convert function
function convert(voiceToUse, textToConvert, response)
{
console.log("Sending Convert Request...");
//data to send as a query
var data = querystring.stringify(
{
username: 'user',
password: 'pass',
action: 'convert',
voice: voiceToUse,
text: textToConvert
});
//options to use
var options = {
host: 'ws.ispeech.org',
port: 80,
path: '/api/rest/1.5',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
//http post request
var req = http.request(options, function (res)
{
res.setEncoding('utf8');
res.on('data', function (chunk)
{
console.log("Body: " + chunk);
var fileId = chunk.substr(chunk.indexOf("fileid") + 7);
console.log("Converting File...");
download(fileId.substr(0, fileId.search("&")), response);
});
});
req.on('error', function (e)
{
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
}
//download function
function download(id, response)
{
//data to send as a query
var data = querystring.stringify(
{
username: 'user',
password: 'pass',
action: 'download',
fileid: id
});
//options to use
var options = {
host: 'ws.ispeech.org',
port: 80,
path: '/api/rest/1.5',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
//http post request
var req = http.request(options, function (res)
{
res.on('data', function (chunk)
{
if (JSON.stringify(res.headers).indexOf("audio/mp3") != -1)
{
console.log("Downloading Chunk...");
/*var fs = require('fs'),
str = 'string to append to file';
fs.open('test.mp3', 'a', 666, function (e, id)
{
fs.write(id, chunk, 0, chunk.length, 0, function ()
{
fs.close(id, function ()
{
});
});
});*/
response.write(chunk, "binary");
}
else
{
download(id, response);
}
});
res.on('end', function ()
{
if (JSON.stringify(res.headers).indexOf("audio/mp3") != -1){
response.end();
}
});
});
req.on('error', function (e)
{
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
}
http = require('http');
fs = require('fs');
server = http.createServer( function(req, res) {
console.dir(req.param);
if (req.method == 'POST') {
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
if(body){
convert('engfemale1', body, res);
res.writeHead(200, {
'Content-Type': 'audio/mp3',
'Content-Disposition': 'attachment; filename="tts.mp3"'
});
}
});
}
});
port = 8080;
server.listen(port);
console.log('Listening at port ' + port);
Javascript code
console.log('begin');
var http = new XMLHttpRequest();
var params = "text=" + bodyText;
http.open("POST", "http://supersecretserver:8080", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//http.setRequestHeader("Content-length", params.length);
//http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
console.log('onreadystatechange');
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);//response text is binary mp3 data
}
else {
console.log('readyState=' + http.readyState + ', status: ' + http.status);
}
}
console.log('sending...')
http.send(params);
console.log('end');
You could try using data URLs:
mp3 download
Not the best browser support though.
It is also possible to use data URLs directly in audio tags.
A better solution would be to save the mp3 on your server somewhere and return a link to it for a mp3 player to use.