Run Node.js server file automatically after launching Electron App - node.js

Im using GitHub Electron to create Desktop application with web technologies.
I'm using Node.js as the server, my problem is that i don't know how to run the file server.js just when launching the electron app.
I want to package my app for distribution so that i can run the server without the command line $ node server.js.

Just simply require the server.js file in your main file (e.g. app.js):
var app = require("app")
, server = require("./server")
;
...
And in the server.js file you can have:
require("http").createServer(function (req, res) {
res.end("Hello from server started by Electron app!");
}).listen(9000)

Related

BASH: express command not recognized

I've read other forums on installing express, I have run
npm install express -g on command prompt as admin.
I did this command a few times and restarted my computer multiple times but Express is still not recognized.
Any help is appreciated.
Express isn't a web server; it is a framework for building web servers.
The express package doesn't provide anything directly executable.
The express documentation has a getting started guide.
If you aren't trying to do server-side programming using Node.js and just want an HTTP server, then the http-server package may be more your speed.
Assuming you’ve already installed Node.js, create a directory to hold your application and make that your working directory.
then follow the below command
npm init
follow step press enter and after
npm install express
make index.js file paste below hello work program.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Now you can run file using below command
node index.js
Open your broswer and enter URL : http://localhost:3000/
You can display Hello World!
You have successfully installed node js and express js program.

How to deploy ReactJS web application on a windows server using Node.JS?

I have developed a ReactJS app using the create-react-app on my local windows 10 machine..
Now, I want to deploy this application on a on-premise windows 2008 R2 server. I do not have much experience with this. When I searched over the internet, all the articles are talking about either deploying into IIS or in cloud. But, I do not want to use IIS and I have On-Premise server.
Is there any other way to deploy and host this ReactJS application on Windows server machine? Maybe just using the Node.JS?
Create the build using npm run build and write a nodejs script with express to serve the build directory.
The nodejs script would be like this:
start-server.js
const express = require("express");
const path = require("path");
const basePath = '';
const app = express();
app.use(basePath + "/", express.static(path.resolve(__dirname + "/build")));
app.get("*", (request, response) => {
response.sendFile(path.resolve(__dirname + "/build/index.html"));
});
app.listen(port);
To start the server:
node start-server.js

How to create Node.js Express app serving a Vue.js SPA?

I'm trying to set up a Node.js project that uses Express to provide a few backend APIs and serve a SPA built with Vue.js.
When I use the Vue cli to initialize a project, I get e.g. src/main.ts main file and commands npm run serve to run a dev server and watch for changes and npm run build to build a production release.
When I use the Express application generator to create a project, I get ./app.js main file and npm start to start the server and watch for changes.
How can I combine these into a single project, both served by the same Express server? Preferably so that a single command would watch + update changes to both server and client? I want to use Vue single file components and TypeScript, which (probably?) require a build step.
(I don't need dynamic server-side rendering of Vue templates, just the static SPA app provided. I prefer TypeScript, but JavaScript answers are fine as well.)
These will be different for your dev and prod environments...
For development look into concurrently - it basically allows you to create a single script in your package.json to start both the client and server concurrently and will watch for changes etc...
For production, you would need something like this in your app.js:
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
(The code above assumes that your directory structure has a client folder with a build folder after having run npm run build I'm more familiar with React than Vue... but the logic should be the same...)

deploying angular universal on node.js express server

angular universal(angular or angular-cli) can be create three part:
browser folder
ng build --prod to generated static file
server folder
A server bundle by script(bundle)
server.js
webpack bundle serve (bundle)
Run node dist/server.js can be work on localhost:4000.
But when I have a node.js(express) server, how can I deploy these files?
If I only have one file by ng build --prod, I can deploy it on node.js like this.
app.js:
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'dist/index.html'));
});
The bundle has its own express embedded, so just start it like you did on your own machine with node server.js.
The recommended way is to use a process manager like pm2. If you want to use pm2, install it with npm i -g pm2 then start your server as a pm2 process with pm2 start server.js

Stop Past Express JS or React Builds from Running

I have been developing an app from the create-react-app starting project.
Today I have been doing the following on my local machine:
deploying my react app using react-scripts start
deploying my react app by using react-scripts build then either serving the build by either...
(A) using the [npm module serve][] as follows serve -p 4001
(B) or attempting to server using a express app like follows:
Express app:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(4001);
I've just restarted my computer and it's still serving the site at:
http://localhost:4001/ and I cannot figure out how to stop it.
I wouldn't mind the continuous deployment of this server but when I build the project again. The changes are not reflected.
The only work around I've come up with is to now deploy at port 4025 and use the Express method coded above.
How the hell can I get rid of this weird residual app that continues to run (via some react process) at port 4001?
I'd really like that port back for sake of keeping it the same across different machines :(
Turns out that chrome keeps react apps running even if the server stops providing them.
Go to chrome://serviceworker-internals and unregister them.

Resources