Linking socket.io file to startup file node js - javascript

I have my start up file and I linked a socket.io file to it, cause my socket.io file is too large it needed to be separate. Everything works good but I require my socket.io file on connection, and I would only like it to be called once instead of it being called every time a page is refreshed or some one goes to another page on my website.
I am looking for another way to require my socket.io file instead of on connection. Here is my code if someone could help me out with this, it would be greatly appreciated.. Thank you for helping...
socketStart = server.listen(8081, function(){
console.log('listening on *:8081');
});
let connectSocket;
connectSocket = exports.connectSocket = require('socket.io')(socketStart);
connectSocket.use(function(socket, next) {
sessionWare(socket.request, socket.request.res, next);
});
connectSocket.on('connection', require('./socketServer.js'));

Related

Trying to display a pdf file in browser

I am new to js.
I am trying to display a pdf file in browser, however I am continuously getting the same respond 'Cannot GET..."
I've tried it different ways.
router.get("/en/tc", function(req, res){
// res.sendFile("/assets/downloads/TC_TS_eng.pdf", {root: "."});
res.download("./assets/downloads/TC_TS_eng.pdf");
})
As well as via 'fileSend'.
It all works well as long as I run it locally. However as soon as I move it to the server it starts returning the above mentioned response.
Any help will be highly appreciated.
You could do this:
app.get("/", function(req, res){
res.sendFile(__dirname + "/test.pdf");
});
What I did here is quite simple. I sent the file when the user goes to the root directory (/) and send the pdf file. __dirname is directory name, which basically makes sure that even when you host on, say heroku, you can still get the file beacuse it gets the full directory path to the file, and then + the pdf name. Hope this helps! Good luck.

Renaming files in a wordpress folder via jquery / jscript

I am trying to run a script that will run every 5 minutes in a shared hostings wordpress folder that will rename the newest CSV file in that folder.
/wp-content/csv/sample.csv
I tried putting a js file in the folder within that folder and run it.
var fs = require('fs');
function runClear()
{
fs.readdir("", (err, files) => {
files.forEach(file => {
console.log(file);
});
})
}
runClear();
setInterval(runClear, 300*1000);
However, it seems like I got client side and server side scripting confused. It seems like I need node.js.
What would be the best approach for this?
Regards,
Yes you are right you are confused in client side and server side script.
Javascript is a client side script which deal with all the user interactions like what will happen user click something or submit a form, hover over some element, scroll the web page etc.
Where as server side script like php deals with data stored on server like mysql records or the physical files.
what you are trying to do is to change the server resource from client side script. and you can not do that directly.
Instead you can call an ajax function which send an HTTP request to some script placed on server. And in that server script write the code to read the existing files in a directory and rename them using file handling operations.

Loading Dygraph input inside a node app

I'm very new to node and am struggling to load the data for Dygraphs on a static page. I believe that my issue is to do with routing. My js for Dygraphs is:
g2 = new Dygraph(
document.getElementById("graphdiv2"),
"../newDataFile.csv",
etc....
I can see in the browser that newDataFile.csv is not being loaded, despite the file being in the root directory.
My routing code looks like this:
router.get('/Dygraphpage', function(req, res) {
res.render('Dygraphpage');
});
I guess that I need to pass the csv file into this routing code, but I don't know how to. Any ideas?!
Figured it out. I am using ExpressJS and the solution was to put the newDataFile.csv inside the Public folder. It then existed at /newDataFile.csv.

How do I send images and javascript files to the browser using Node js

I have created a Node.js server that worked well when the Javascript was part of the HTML page. I moved the JS to another file and added some images. Now it won't load the images or the JS into the browser. However, the web page renders perfectly when I open the web page directly. This is what my server looks like:
app.get('/',function(req,res){
res.end('Hello World!Go to /map to see the google map');
});
app.get('/map',function(req,res){
var conn;
//images must be sent to the client from the server...
res.sendfile(__dirname+'/client/google_maps.html');
//receiving requests from jQuery
});
I am not using the Express project structure or the Express middleware or Express configuration to do this.
If that's all your code, I think the problem you met is reasonable. You didn't tell your server how to respond your images and scripts when browser requested. For example in your google_map.html file you have <script src="myjs.js"></script>, then your browser will ask your node application to give the content of myjs.js but your server don't know how to deal with it.
You could try to add code like below to see if it helps.
app.get('/myjs.js', function (req, res) {
res.sendfile(__dirname + '/myjs.js');
});
As dimadima said, Express provides a module to handle static files that you can use like
app.use(express.static(__dirname + '/public'));

Is there anyway to copy/upload/edit a file with javascript?

I have two servers, Server1 & Server2. I want to copy a file from Server1 to Server2 with JavaScript. Is this possible? If so, how?
For example, last week I used "wget" command for this action. Now I want to handle it with JS.
i don't know the full specifications for the task at hand, but you could look into using Node.js to assist with your issue. here's a quick repo that might help repo or you could use this snippet i took from similar post:
var http = require('http');
var fs = require('fs');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/',
{'host': 'www.google.com'});
request.end();
out = fs.createWriteStream('out');
request.on('response', function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
out.write(chunk);
});
});
i hope this helps, and here's the original post
Nope. You can't access disk from JavaScript. Just for a moment think of security problems it can bring with itself. I simply create a web page, and when you visit it, I upload all the images of your girlfriend and publish them (just kidding, but that's the security problem it poses).
However, JavaScript can access files on some scenarios:
When user selects some files using <input type='file' /> element
Using HTML5's offline-storage (I guess this one, not sure).
However, if you want, you can use Node.js to do that. However, this is a server-side stuff.

Categories