I have a very simple node.js app which successfully processes a file (from formidable form) but the download of the result file fails. The download html
<p><input type="text" value = "tx-AllSerums.xlsx"><br>
<a href="/transformed/tx-AllSerums.xlsx"
download="All Serums.xlsx">Download transformed file</a>
</p>
The result files are written to the directory structure from where I run node (the 'transformed' dir is in the dir in which I run node).
When I click on the above href I get console output but no file save dialog. (Should the server even get this event?)
When I kill the node server, up pops the file save dialog. Of course the save fails: Network error.
I'm currently running everything on my workstation: Ubuntu 18.04; Chrome 70.0.3538.77, node v10.13.0
I've found that the file save dialog does appear but a very long time after the click. But the server is still up and the save operation still fails: Network error.
Adding res.end() to the block catching the download click gets the dialog up immediately. Now I successfully download a zero length file. Oddly I find that encouraging.
var formidable = require('formidable');
var util = require("util");
var http = require('http');
var url = require('url');
var fs = require('fs');
var java = require("java");
var proc = require("process");
java.classpath.push("/home/u0138544/fake/prep-1.jar");
java.classpath.push("/home/u0138544/fake/SRTDependancies-1.jar");
console.log(proc.cwd());
http.createServer(function (req, res) {
txName = "";
if (req.url == '/fileupload' && req.method.toLowerCase() == 'post') {
var form = formidable.IncomingForm();
form.keepExtensions = true;
var excelFilePath;
var origName;
form.parse(req , function (err, fields, files) {
excelFilePath = files.filetoupload.path;
console.log("upload " + excelFilePath);
origName = files.filetoupload.name;
txName = "tx-" + origName.replace(/\s/g, "");
console.log( "txname = " + txName);
//just for formatter
var clz = "edu.utah.camplab.xlsx.SampleReportTransformer";
var tx = java.newInstanceSync(clz);
java.callMethodSync(tx, "run", excelFilePath, "./transformed/" + txName);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<p>');
res.write('<input type="text" value = "' + txName +'"><br>');
res.write('Download transformed file</p>');
return res.end();
});
}
else if (req.url.includes('transformed') &&
req.method.toLowerCase() == 'get') {
console.log("download attribute doesn't work")
res.end();
}
else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload" label="Pick a file"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(15080);
Related
For my homework problem, I need to check if a file exists in a certain
directory "public", and then if it does, open it up through the
localhost in a browser. I wanted to use a function to check if the file
exists in directory, and then if true, send the file path to another function that'll open the file. This is all on my server.js file, and the HTML file I want to open along with the CSS and JS for the HTML file are all in my public directory.
This is using node.js. I need to run node server.js in the console, and then go to my localhost in a browser and see the displayed webpage with style and functionality. I've tried using fs.access and fs.existsSync. We are not allowed to use express on this assignment.
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
var filepath = 'public' + req.url;
if (req.url == '/index.html' || req.url == '/') {
fs.access(filepath);
}
}).listen(3000);
console.log("Server running on Port 3000...");
fs.access(filepath, (access) =>{
if (access) {
res.statusCode = 200;
sendFile(filepath, res);
}
else {
res.statusCode = 404;
sendFile('public/404.html', res);
}
});
function sendFile(path, res) {
fs.readFile(path, "utf8", function(data){
res.end(data);
});
}
I'm getting an error that says filepath is not defined (when I use it in the parameters when I call fs.access.
fs requires paths with a leading slash to work, so instead of var filepath = 'public' + req.url;, try
var filepath = '/public' + req.url;
I am trying to create a chat website, something like a discord clone. I am using socket.io to connect my front end and back-end but I cant figure out how to make it that when someone enters a message that message to be displayed on all currently open browser pages
Server.js (My server file I use):
const express = require("express");
const app = express();
const http = require("http").Server(app);
const port = 4000;
const io = require("socket.io")(http);
app.get("/", (req, res) => {
res.sendFile("ChatRoom.html", {"root": __dirname});
});
io.on("connection", (socket) => {
console.log("A user has connected");
socket.on("messageSend", (data) =>{
console.log(data);
io.emit("chatUpdate", data);
});
});
http.listen(port, () => {
console.log("Server.js listening on port " + port );
});
And my Javascript code in the HTML file:
var socket = io();
document.addEventListener('keydown', InputText);
function InputText(e)
{
//Checks if the pressed button is Enter and if the input box is empty
if( e.keyCode == 13 && document.getElementById("chat_input").value != "")
{
//Gets the div which the message will be ridden to
var parent = document.getElementById("chat");
//Current date to be used when displaying the exact time of sending the
messgae
let d = new Date()//.getTimezoneOffset();
//Getting the properties of the input
var value = document.getElementById("chat_input");
//Telling the server that a message has been sent - function
emitter(parent, value.value, d);
//Setting the text box back to blank
value.value = "";
}
}
//Function
function emitter(holder, text, date){
socket.emit("messageSend", text);
socket.once("chatUpdate", (message) => {
var z = document.createElement("p");
z.innerText = date.getHours() +":"+ date.getMinutes() + " | " +
message;
z.style = 'border-top: 1px solid Black;border-bottom: 1px solid
Black;font-size:20px; margin: 0;padding: 10px;';
holder.appendChild(z);
});
}
Once you have emitted the message you need to be listening for it in the client, if the action you are trying to achieve is sending the message to another user that has the socket currently open on their window. Because you are sending it to a user not the server, so you would need to be connecting to the socket so it's very possible your socket might need to be defined more like this.
var socket = io.connect('Your:/url/of/windowlocation/whileonsocket/here')
Hope this helps!
I am working with a data feed that sends a JSON stream over a TCP socket and I'm using node.js/socket.io to emit the stream to a browser client.
Everything is working except I need each JSON object to emitted as a separate message from the socket.io server. In the client the messages are received like this:
//Message 1:
{"type":"TYPE_1","odds":[{"eventId":"foo","odds":[{"oddId":foo,"oddType":"LIVE","source":"foo"}]}]}
//Message 2:
{"type":"TYPE_2","odds":[{"eventId":"foo","odds":[{"oddId":foo,"oddType":"LIVE","source":"foo"}]}]}
{"type":"TYPE_3","odds":[{"eventId":"foo","odds":[{"oddId":foo,"oddType":"LIVE","source":"foo"}]}]}
//Message 3:
{"type":"TYPE_4","odds":[{"eventId":"foo","od
//Message 4:
ds":[{"oddId":foo,"oddType":"LIVE","source":"foo"}]}]}
The data feed docs state: "All messages sending to your application will form a JSON stream (no delimiter between messages), so you may need a decoder that support JSON stream."
So the stream is strictly correct but I need each object as separate message.
I have looked at https://www.npmjs.com/package/JSONStream and others but am very new to nodejs and socketio and am not sure how to implement them in to the server.
Have also read How can I parse the first JSON object on a stream in JS, nodejs JSON.parse(data_from_TCP_socket), http://www.sebastianseilund.com/json-socket-sending-json-over-tcp-in-node.js-using-sockets.
I think it's something to do with buffer chunk lengths and them being too big so the messages get split but that could be wrong! I'm guessing I need a delimiter check that balances brackets but not sure how to go about it or if the right approach.
My Server Script:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var net = require('net');
var port = 8992; // Datafeed port
var host = '127.0.0.1'; // Datafeed IP address
//Whenever someone connects this gets executed
io.on('connection', function(socket){
console.log('A user connected to me the server');
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
//Create a TCP socket to read data from datafeed
var socket = net.createConnection(port, host);
socket.on('error', function(error) {
console.log("Error Connecting");
});
socket.on('connect', function(connect) {
console.log('connection established');
socket.write('{"type":"SUBSCRIBE"}');
});
socket.on('data', function(data) {
//console.log('DATA ' + socket.remoteAddress + ': ' + data);
var data = data.toString();
io.sockets.emit('event', JSON.stringify(data));
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
My Client:
<!DOCTYPE html>
<html>
<head><title>Hello world</title></head>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
</script>
<body>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<ul id="messages"></ul>
<script>
socket.on('event', function(data){
var t = JSON.parse(data.toString('utf8'));
$('#messages').prepend($('<li>').text(t));
console.log('Got event from Server:', t);
});
</script>
</body>
</html>
Any help or guidance would be amazing as really struggling with this.
A common delimiter to use is a newline character (\n). If you have that appended to your JSON messages it will be very easy to parse the messages. For example:
var sockBuf = '';
socket.setEncoding('utf8');
socket.on('data', function(data) {
sockBuf += data;
var i;
var l = 0;
while ((i = sockBuf.indexOf('\n', l)) !== -1) {
io.sockets.emit('event', sockBuf.slice(l, i));
l = i + 1;
}
if (l)
sockBuf = sockBuf.slice(l);
});
or a more efficient, but slightly less simple solution:
var sockBuf = '';
socket.setEncoding('utf8');
socket.on('data', function(data) {
var i = data.indexOf('\n');
if (i === -1) {
sockBuf += data;
return;
}
io.sockets.emit('event', sockBuf + data.slice(0, i));
var l = i + 1;
while ((i = data.indexOf('\n', l)) !== -1) {
io.sockets.emit('event', data.slice(l, i));
l = i + 1;
}
sockBuf = data.slice(l);
});
I am using IBM Bluemix to make a web service for a school project.
I need to transform the .csv data I have in a directory in my computer into a .json file, so I can manipulate this information.
I am using the fast-csv package for Node JS, but I am having trouble with the code.
In the end of my .js file, there is a piece of code that is supposed to get the .csv file and convert it to JSON. I obtained it in the fast-csv documentation webpage.
When I run it, nothing happens and I can't fin out why. How can I check if the code is really getting the .csv file and transforming it into a .json one?
The Cambio.csv file is in the same directory of my .js one.
// Hello.
//
// This is JSHint, a tool that helps to detect errors and potential
// problems in your JavaScript code.
//
// To start, simply enter some JavaScript anywhere on this page. Your
// report will appear on the right side.
//
// Additionally, you can toggle specific options in the Configure
// menu.
function main() {
return 'Hello, World!';
}
main();/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// HTTP request - duas alternativas
var http = require('http');
var request = require('request');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
//chama o express, que abre o servidor
var express = require('express');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
app.get('/home1', function (req,res) {
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) {
var body = '';
res2.on('data', function (chunk) {
body += chunk;
});
res2.on('end', function () {
var json = JSON.parse(body);
var cotacao = json["bovespa"]["cotacao"];
var CotacaoDolar = json["dolar"]["cotacao"];
var VariacaoDolar = json["dolar"]["variacao"];
var CotacaoEuro = json["euro"]["cotacao"];
var VariacaoEuro = json["euro"]["variacao"];
var Atualizacao = json["atualizacao"];
console.log('url', req.originalUrl);
obj=req.query;
DolarUsuario=obj['dolar'];
RealUsuario=Number(obj['dolar'])*CotacaoDolar;
EuroUsuario=obj['euro'];
RealUsuario2=Number(obj['euro'])*CotacaoEuro;
if (VariacaoDolar<0) {
recomend= "Recomenda-se, portanto, comprar dólares.";
}
else if (VariacaoDolar=0){
recomend="";
}
else {
recomend="Recomenda-se,portanto, vender dólares.";
}
if (VariacaoEuro<0) {
recomend2= "Recomenda-se, portanto, comprar euros.";
}
else if (VariacaoEuro=0){
recomend2="";
}
else {
recomend2="Recomenda-se,portanto, vender euros.";
}
res.render('cotacao_response.jade', {
'CotacaoDolar':CotacaoDolar,
'VariacaoDolar':VariacaoDolar,
'Atualizacao':Atualizacao,
'RealUsuario':RealUsuario,
'DolarUsuario':DolarUsuario,
'CotacaoEuro':CotacaoEuro,
'VariacaoEuro':VariacaoEuro,
'RealUsuario2':RealUsuario2,
'recomend':recomend,
'recomend2':recomend2
});
var csv = require("fast-csv");
csv
.fromPath("Cambio.csv")
.on("record", function(data){
console.log(data);
})
.on("end", function(){
console.log("done");
});
});
});
});
The code did not work because it was missing views engine setup.
After adding the following lines after var app = express();:
// view engine setup
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
and creating a sample Cambio.csv on the application root directory (same dir as app.js):
$ more Cambio.csv
mercado,cotacao,variacao
bovespa,1234,-2.3
dolar,3.777,0.23
euro,4.233,0.12
I run node app.js and point my browser to:
http://http://localhost:6006/home1
Output was:
$ node app.js
server starting on http://localhost:6006
url /home1
[ 'mercado', 'cotacao', 'variacao' ]
[ 'bovespa', '1234', '-2.3' ]
[ 'dolar', '3.777', '0.23' ]
[ 'euro', '4.233', '0.12' ]
I am new to node.js
I was just making an simple application
my data is inserting properly into the database as well as fetching also from the database
But the problem is when I am trying to access it in json model it is giving me error
var express = require('express');
/*
* body-parser is a piece of express middleware that
* reads a form's input and stores it as a javascript
* object accessible through `req.body`
*
* 'body-parser' must be installed (via `npm install --save body-parser`)
* For more info see: https://github.com/expressjs/body-parser
*/
var bodyParser = require('body-parser');
// create our app
var app = express();
// instruct the app to use the `bodyParser()` middleware for all routes
app.use(bodyParser.urlencoded({ extended: true }));
// A browser's default method is 'GET', so this
// is the route that express uses when we visit
// our site initially.
app.get('/', function(req, res){
// The form's action is '/' and its method is 'POST',
// so the `app.post('/', ...` route will receive the
// result of our form
var html = '<form action="/" method="post">' +
'Enter your name:' +
'<input type="text" name="userName" placeholder="Put your name" />' +
'<br>' +'Enter your city:'+'<input type="text" name="userCity" placeholder="Put your city" />' +
'<br>' +'Enter your state:'+'<input type="text" name="userState" placeholder="Put your state" />' +
'<br>' +'Enter your country:'+'<input type="text" name="userCountry" placeholder="Put your country" />' +
'<br>' +
'<button type="submit">Submit</button>' +
'</form>';
res.send(html);
});
// This route receives the posted form.
// As explained above, usage of 'body-parser' means
// that `req.body` will be filled in with the form elements
app.post('/', function(req, res){
var userName = req.body.userName;
var userCity = req.body.userCity;
var userState = req.body.userState;
var userCountry = req.body.userCountry;
// var document = {userName:userName,userCity:userCity,userState:userState,userCountry:userCountry};
var html = 'Hello: ' + userName + '.<br>' +'City: ' + userCity + '.<br>'+'State: ' + userState + '.<br>'+'country: ' + userCountry + '.<br>'+
'Try again.';
// res.send(html);
//res.send(JSON.stringify(doc));
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = require('mongodb').MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
/* var userName = req.body.userName;
var userCity = req.body.userCity;
var userState = req.body.userState;
var userCountry = req.body.userCountry;
var document = {userName:userName, userCity:userCity,userState:userState,userCountry:userCountry};*/
// do some work here with the database.
var insertDocument = function(db, callback) {
db.collection('test').insertOne( {
"userName" :userName,
"userCity" : userCity,
"userState" : userState,
"userCountry" :userCountry ,
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the test collection.");
callback(result);
});
};
var findDocument = function(db, callback) {
var cursor =db.collection('test').find( );
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
console.log(doc);
// res.contentType('application/json');
res.send(JSON.stringify(doc));
/* app.get('/test', function(req, res, next) {
res.json(doc);
});*/
} else {
callback();
}
// res.send(JSON.stringify(doc));
});
};
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
findDocument(db, function() {
db.close();
});
});
});
/* var document = {name:"David", title:"About MongoDB"};
db.collection('test').insertOne(document, function(err, records) {
if (err) throw err;
console.log("Record added as "+records[0]._id);
});*/
//Close connection
// db.close();
}
});
});
app.listen(3000);
Please help me to get rid off the problem.
Thank you..
The error I am getting is cann't set headers after they are send
This kind of error usually means that you try using res.send(...) multiple times from the same route.
Here you can see that in your find document, you use a cursor.each, and send your result inside this cursor.each. This means that you send multiple results from the same route.
What you should do instead is having a variable that you use to store your result before sending it once everything is retrieved.