electron with nodejs and angular - node.js

I am trying to set up Angular 5 project in Electron and also run nodejs in server side, i know that electron permit to create desktop app, but is it possible to create server side app with electron, also i'm not sure if it's possible to have the nodejs app and angular all in the same project with ngx electron.

If I understand you correctly, you want a desktop application that in some way also communicates with a server.
Sure, that's possible since Electron will bundle node.js in the generated binary.
The easiest way I can think of is setting up an HTTP API (e.G REST) on your server side (possible with any language, I'd recommend either PHP or Node.js) and use http in your ng/electron app to talk to the server.
http.get(options, (res) => {
// Do stuff
}).on('socket', (socket) => {
socket.emit('agentRemove');
});

Related

How to run a gRPC Server and an Express Server on the Same PORT in cloud run

Are there any known techniques or hacks for running both an Express Server and a GRPC Server on the same port? I am aware Cloud Run exposes just a single Port for a service instance.
So, I'm literally just looking for hacks as it stands now.
Like below
const app = express();
const port=process.env.PORT;
app.listen(port,()=>{});
gRPCServer.bindAsync(`0.0.0.0:${port}`, grpc.ServerCredentials.createInsecure(), () => {
gRPCServer.start();
});
I wish to expose some routes to my users via express and only use GRPC for internal micro services communication.
I saw https://github.com/grpc-ecosystem/grpc-gateway but there are very few documentations on how to use it with NodeJS plus I DON'T want to generate client libraries. I prefer dynamic code generation.

Flutter web app fails to get node API on the same pc

I have created a flutter web app which uses NodeJS API to fetch data from mongodb database and write some data to mongodb. The app works fine when i run it as a Desktop app,it communicates to my node server. The app also work fine when i run it on chrome using
flutter run -d chrome
from android studio terminal. But when i build the web app and run it with the bellow command only the static page loads and it is not fetching data from mongoDB. It isnot communicating with NodeJS API.
flutter build web
cd build/web
python -m http.server 8000
So all i want was to deploy my flutter web app to a server and check if it works properly, but it fails to connect to the NodeJS back end. What is the best way to do it,Deploy a Flutter web app with also uses NodeJS APIs to fetch some JSON data.
Enable CORS for API. Here is an example which enables it for all requests.
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
...
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
See cors for details.

Run both client and server apps at same time IIS Express for local development

I have a Visual Studio 2019 Project that has both a ReactJS client app in ClientApp folder and a .Net Core 3.1 Entity Framework app in the same solution. The client side ReactJS app uses the controllers on the .NetCore side to manage the data.
For example, one way the ReactJS Client-side app gets data from the .NetCore API is like this:
const getPlayerStats = async (onlineVal) => {
const apiResult = await axios("api/gamerStats/", {
params: {
isOnline: onlineVal
}
});
return apiResult;
};
On my Windows Server using IIS, it runs both fine.
However, when I try and debug the solution locally, it fires up IIS Express but only the .Net Core Entity Framework is running.
If I start the app using npm init from the ClientApp folder, it starts the ReactJS part, but then the .NetCore Entity Framework app is not running.
How can I debug locally so that both parts of the solution are running and can talk to each other?
Thanks!

How to run Node.js server in Ionic mobile app?

I am making an app using MEAN and ionic framework where nodejs is a middleware to connect to the database(mongoDb). I need to run the nodejs server using node server.js and the app using ionic serve. This is my server.js.
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
CohortController =require('./www/server/controller/CohortController');
mongoose.connect('mongodb://localhost:27017/persistent');
app.use(bodyParser());
app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);
app.listen(3000,function(){
console.log('Listening...');
})
Now this is my app.js. I use http://localhost:3000 to get the JSON.
app.controller('CohortController',['$scope','$resource',
function($scope,$resource){
var Cohort=$resource('http://localhost:3000/api/cohorts');
Cohort.query(function(results){
$scope.cohorts=results;
});
$scope.cohorts=[];
$scope.createCohort= function () {
var cohort=new Cohort();
cohort.name=$scope.CohortName;
cohort.id=$scope.CohortId;
cohort.$save(function(result){
$scope.cohorts.push(result);
$scope.CohortName='';
$scope.CohortId='';
});
}
}]);
How can I run the node server when I convert it into a mobile application? How the application will use the API?
You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it's public IP. So, you wouldn't use http://localhost:3000 to get the JSON, instead you would use something like http://123.456.789.123:3000.
But, usually, this is not the way you would do it (with the port 3000). What you would additionally do is put (for example) Nginx in front of your Node.js app (see an example here) in order to serve your api from the standard HTTP port (80).
So, basically, you can't actually "run Node.js server in Ionic app" - the way you do it is run the Node.js app separate from Ionic and expose its functionality via a standardized API (usually these days REST is what you would want to achieve) which you then "consume" via Ionic's (well, to be exact, it's actually Angular's) $resource module.
Hope this helps clear things up a bit.

Firebase Hosting with own server node.js

I have webapp with firebase database. I would like hosting the app on firebase. My app has own server nodejs and using websockets. How can I host my app on Firebase? And how can I run my own server on Firebase?
I think your question is quite simple. And the answer is also simple: no, you can't.
Firebase only serves static files. You need to try heroku, codeship, etc for that.
I'm not sure what exactly you are looking for. I'll assume it's one of these two:
you want to run the node.js scripts on Firebase's server
There is no way to run your own code on Firebase's servers.
you want to run the node.js scripts on your own server and have them interact with your Firebase data
Firebase has a node.js package that allows you to talk to its BaaS service from your own node scripts. See the node.js section in Firebase's quickstart and the npm package for Firebase.
You can use Google Cloud Functions to do most task processing in a serverless style: https://firebase.google.com/docs/hosting/functions
I'm using it to dynamically load javascript based on req.url.
With Firebase functions, yes you can. You can watch this tutorial from Google, it's very clear and easy to catch up.
Node.js apps on Firebase Hosting Crash Course - Firecasts
Firebase Hosting allows you to use Cloud Functions to perform server-side processing. This means that you can support dynamic generation of content for your Firebase Hosting site.
Documentation
Firebase Functions is the way to go.
Example:
Setup /index.js in your project with expressjs listening on port what ever you want. F.e. 3000
const app = express()
const port = 3000
...
app.get('/', (req, res, next) => {
... hier your code to handle request
})
...
app.listen(port, () => {
console.log(`app listening at port = ${port}`)
functions.logger.info("Application started", {structuredData: true});
})
Export your function with reference to express-app:
exports.api = functions.https.onRequest(app)

Resources