download file with nodejs and sails - javascript

i want to download file that i upload to the server with node js and framework sailsjs
$scope.download = function(l,n){
console.log(n);
var fs = require('fs');
var http = require('http');
var file = fs.createWriteStream(n);
var request = http.get(l, function(response) {
response.pipe(file);
});
}
and when i click to download this error message i get:
ReferenceError: require is not defined
in this 2 file :
var fs = require('fs');
var http = require('http');
and i install before fs and http in npm install
thanks for the help

require() method is a method to import other files into your code in node.js.
node.js is server side javascript platform, which means it only runs on the server side. you're trying to write server side code on the client.
I can see you are using angular. if you're trying to make http requests to your server take a look at this: https://docs.angularjs.org/api/ng/service/$http

Related

Convert Nodejs server into node module

Is it possible to convert a nodejs API server into a node module, which can be used for other projects without making much code changes?
Details: There are several API's(get, post, put) in the node js server. So if I use this server as a node module inside another node server, I must be able to access the API's in the node modules directly from the client. Is this possible? If yes, how?
I am required to do a POC on this for a client requirement and so far did not find it possible. Can you please help? I am relatively new to node js development
main script
const express = require('express');
const app = express()
/*
use some middlewares
*/
require('my-module')(app)
const server = http.createServer(app).listen(process.env.PORT);
module.exports = app;
module
module.exports = (app) =>{
app.get('/',(req,res) =>{
res.send('hi im a diffrent module;')
}
}

read / write file of remote system in javascript

I got a task to make a button on web page and by clicking on that button I have to read and write a file of local system. How it can be possible.?
Can it be done in node.js or can it be possible if i make any browser app.
Kindly help.
You can read or write files using Nodejs using fs module.
To write file in Node js:
var fs = require('fs');
fs.writeFile(filename, data, [encoding], [callback])
To Read file in Node js :
var fs = require('fs');
fs.readFile(file, [encoding], [callback]);
Your browser doesn't have permission to edit files, you can run a javascript in node.js to read and write files.
var fs = require('fs');
var file = fs.readFile('package.json','utf-8');
console.log(file);

Download game with electron / detect game version with electron

I have to build a game launcher with electron. I have two questions:
Which way to download file from client (angularjs)? ftp? http?
how can I detect the game version to update it?
With electron you can use all the APIs Node.js have additional to the APIs Chrome have.
So you can Download the game useing ftp or http like you would do in Node.js or use Ajax ($http).
For saveing you can use the normal file system, and for the version you can use the filesystem or localstorage.
Here is a snippet to save the game:
const http = require('http');
const fs = require('fs');
const app = require('remote').require('app');
var file = fs.createWriteStream(app.getDataPath() + "externalFiles/game.zip");
var request = http.get("http://dl.example.com/game.zip", response => {
response.pipe(file);
});
on the server you can simply have a request returning the version or the hash of the latest version, and if that changes it will download the game again.

Why do I get a Bad request when I run server.js and client.js and access localhost?

I am new to node.js
I am trying to use the pub/sub faye nodemodule to create a publish/subscribe web application.
I have the following files and folders: My dependencies in the node_modules folder, a sever.js file, and a client.js
My server.js contains the following code:
var http = require('http'),
faye = require('faye');
var server = http.createServer(),
bayeux = new faye.NodeAdapter({mount: '/'});
bayeux.attach(server);
server.listen(8000);
As for my client.js file, it contains the following code:
var faye = require('faye')
var client = new faye.Client('http://localhost:8000/');
client.subscribe('/messages', function(message) {
alert('Got a message: ' + message.text);
});
client.publish('/messages', {
text: 'HAI!'
})
I go to my terminal and run node server.js and node client.js
When I go to my browser and run localhost:8000, all I get is a Bad request.
Why am I getting this? I am suspecting I don't have any page to display something.
Could someone please point out to me what I am missing.
Thanks.
You're getting that error because you've mounted Faye at /. So if you try browsing to /, the server is expecting a Faye client, not a normal HTTP request.
There is a browser client example that shows how to connect with Faye mounted at /faye. You can also pass in a request handler into http.createServer() that allows you to handle the request before Faye does, in case you want to respond to the particular request.

Socket IO V0.7: Where to put flashsockets SWF file?

I currently have my 'WebSocketMain.swf' file sitting in the same directory as 'socket.io.min.js' but Firefox doesn't seem to want to use flash sockets. It always reverts to XHR-Polling. See test case here : http://thebeer.co/labs/rt/test.php (page is blank, check JS console for feedback).
Is this the right place for it?
Do I need to direct Socket.io to the location of this SWF file?
UPDATE:
My node server requesting minified client js.
var $ = require('jquery');
var http = require('http'),
url = require('url'),
https = require('https'),
fs = require('fs'),
crypto = require('crypto'),
io = require('../'),
sys = require(process.binding('natives').util ? 'util' : 'sys'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
});
server.listen(80,"173.201.181.56");
var io = io.listen(server), buffer=[];
io.set('browser client minification', true);//<<minified client js requested here.
My client side including the minified JS:
<script src="http://173.201.181.56:60/socket.io/socket.io.js"></script>
I see that you decided to host the file your self. Did you know that Socket.IO also serves the client for you? See https://github.com/LearnBoost/Socket.IO/wiki/How-do-I-serve-the-client
You can even configure it, so it outputs a minified build: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
This client also knows where the location of the .swf file is, so you don't need to configure anything.
If you still want to serve the file your self (which is not recommended) You need to set the window.WEB_SOCKET_SWF_LOCATION to http://yoururlhere.com:port/socket.io/static/flashsocket/WebSocketMain.swf or WebSocketInsecure.swf (this depends if you go cross domain or port, but the bundled socket.io client handles this already for you)

Categories