Download game with electron / detect game version with electron - javascript

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.

Related

How to connect a Postgres Database to a HTML page with Node.js? [duplicate]

First I dowloaded nodejs from link.
Then I installed browserify npm install -g browserify
Then I installed fs npm install fs
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {return console.log(err);}
console.log("The file was saved!");
});
</script>
</body>
</html>
I am getting the following error:
Uncaught ReferenceError: require is not defined
at index.html:12:12
Why require is still not defined? What could I do to get the code to be executable?
Node.js is a stand-alone environment for running JavaScript. It is not a browser extension.
To run Node.js code, save it in a file with a .js file extension, then run it with:
node yourFile.js
If you want Node.js code and browser code to interact then the typical way is to write a server in Node.js that hosts a web service. Express.js is a popular way to do this. You can then use Ajax to make HTTP requests to that web service.
For bi-directional communication (as opposed to the request/response of Ajax) look at Socket.io.
I just want it to be local storage, so I do not want to share the datas between the users, I just want to interact with there own local storage datas.
If you want to use the localStorage API provided by browsers, then do so.
localStorage doesn't need Node.js. It doesn't need the fs module that is built into Node.js. It doesn't need the require method that is part of the CommonJS module specification.
You can't muck around freely on the user's file system from code running in the browser. That would be a huge security problem.
You can't use node APIs in browser environment; more specifically browser won't allow script to directly interact with file APIs like Node.
Either run your code in node as quentin said, or use browser file api

Use home network for learning browser-server communcation

I'm really new to html, css, javascript and all kind of web development. After I coded my first own website, I'd like to play a little with communcation between the browser and a server.
I'd like to be able to access a website hosted on my PC (using my PC as "Web" server) over my home network (my router) so that I can load and use it for example on my smartphone. There's no need for it being accessible over the internet, I only want to use it at home when being connected to my WiFi.
Is that possible?
You can very simply build your own server using Node.js and Express.js.
Here is a simple snippet to create a server on port 8001 on your PC.
const express = require("express");
const app = express();
app.listen(8001, () => {
console.log("Listening on port 8001");
});
After that, you start building routes, middleware, and static files for your server. More info can be found here and here.

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;')
}
}

download file with nodejs and sails

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

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