Node.js - How to run multiple servers using package.json - javascript

Trying to run multiple separate servers in parallel from package.json. This method only runs server1.js, but ignores server2.js
Package.json
{
"name": "demo",
"version": "1.0.0",
"description": "demo",
"main": "server1.js",
"dependencies": {
"express": "^4.14.0",
"socket.io": "^1.5.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server1.js && node server2.js",
}
}
P.S -- code will be used in AWS

In the shell, && is a command that executes the next command after the previous command is executed, and if the state is true.
If you want multiple servers to run in parallel at the same time, use the background process commands, & or nohub.
node server1.js & node server2.js &
and now, your application runs in the background, and the process information can be obtained with the command below.
If you want to stop this, get pid information with netstat command
You must delete it with the command kill -9 <your_procees_id>.
netstat -vanp --tcp | grep <your_server_port>
kill -i <your_process_id>
Alternatively, there may be a monitoring module such as pm2.
https://www.npmjs.com/package/pm2

Related

Websocket connection to 'wss://url' failed : [vite] server connnection lost. polling for restart... after deploying the vitejs project in AWS EC2

I have created the vite project it is working perfectly fine in the localhost. But when my project was deployed in AWS EC2 instance, I am getting following error message :
Websocket connection to 'wss://www.domain.com/3000' failed:
[vite] server connnection lost. polling for restart...
pacakge.json :
{
"name": "screen-recording-demo",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"vite": "^2.0.5"
},
"dependencies": {
"firebase": "^8.2.10"
}
}
I have used firebase dependencies in my project in locally everything is working fine. I have checked the vite doumentation, but not getting what needs to be added or I am missing in the production.
based on my experience this a day ago, i need to open the port used
in Nuxt 3 it's 24678, so i must execute these
ufw allow 24678/tcp

How to fix Proxy server error on Windows 10?

I ran into a proxy problem in my package.json file.
"dev": "set PROXY = true && node server.js",
"dev-saas": "set PROXY = true && node server.js",
On Windows, this code does not start the program and gives an error
'PROXY' is not recognized as an internal or external command,
operable program or batch file.
After doing a little google search, I changed the code to this
"dev": "set PROXY = true && node server.js",
"dev-saas": "set PROXY = true && node server.js",
and wrote a little below:
"proxy": {
"target": "http: // localhost: 3001"
},
After that, the code works, but I don't think it's correct, because it gives an error:
Error: [HPM] Missing "target" option. Example: {target: "http://www.example.org/%22%7D

How to connect to openVpn in gitlab pipeline for a cypress project

I am working on a cypress project. I have set up a pipeline in GitLab.
My application only works over private network connected via Open VPN.
Can some one guide me how to add that in .gitlab-ci.yml file ???
My .gitlab-ci.yml is :
image: cypress/base:10
stages:
- test
test:
stage: test
script:
- npm install
- npm run test
and my package.json is as follows:
{
"name": "cypresspackage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"clean:reports": "rm -R -f cypress/reports && mkdir cypress/reports && mkdir cypress/reports/mochareports",
"pretest": "npm run clean:reports",
"scripts": "cypress run --spec cypress/integration/dummy.feature",
"combine-reports": "mochawesome-merge ./cypress/reports/mocha/*.json > cypress/reports/mochareports/report.json",
"generate-report": "marge cypress/reports/mochareports/report.json -f report -o cypress/reports/mochareports",
"report:copyScreenshots": "cp -r cypress/screenshots cypress/reports/mochareports/assets",
"posttest": "npm run report:copyScreenshots && npm run combine-reports && npm run generate-report",
"test": "npm run scripts || npm run posttest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"cypress": "^6.3.0",
"cypress-audit": "^0.3.0",
"cypress-cucumber-preprocessor": "^4.0.1",
"cypress-multi-reporters": "^1.4.0",
"cypress-xpath": "^1.6.2",
"mocha": "^8.2.1",
"mochawesome": "^6.2.1",
"mochawesome-merge": "^4.2.0",
"mochawesome-report-generator": "^5.1.0"
},
"dependencies": {
"lambdatest-cypress-cli": "^1.0.1"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true
}
}
I guess gitlab provides the runner at run time
I guess you are using GitLab's SaaS. This means your VPN would be opened in a non-private environment. For example, some GitLab admins should have access to your VPN connection and, depending on how GitLab is configured in their backyard, some other GitLab users may have access to your private network. I'd avoid that. If you insist on that, you'd better use your project's Secrets feature to save your OpenVPN client authentication, so it remains private.
Is there a option where I can choose the runner?
Sure. You can register a runner running on your own servers (or even at home on-demand). It depends on where and how this runner is being used (Docker? Kubernetes? Debian? etc). Take a look into Registering a GitLab Runner. You'll need to generate a token from your project's configuration and then install the runner using that token.
GitLab CI
Once you have your own runner installed and configured (ensuring it runs when needed), you'll need to configure your VPN start/stop in the pipeline. Here, I copy a piece of code found on GitLab's forum:
before_script:
##
## VPN
## Inspiration from: https://torguard.net/knowledgebase.php?action=displayarticle&id=138
## And http://forum.gitlab.com/t/connect-vpn-during-ci-cd/7585
## Content from Variables to files: https://stackoverflow.com/a/49418265/4396362
## Waiting for opnevpn connect would be better than sleeping, the closest would be https://askubuntu.com/questions/28733/how-do-i-run-a-script-after-openvpn-has-connected-successfully
## Maybe this would work https://unix.stackexchange.com/questions/403202/create-bash-script-to-wait-and-then-run
##
- which openvpn || (apt-get update -y -qq && apt-get install -y -qq openvpn) # Install openvpn if not available.
- cat <<< $CLIENT_OVPN > /etc/openvpn/client.conf # Move vpn config from gitlab variable to config file.
- cat <<< $VPN_U > /etc/openvpn/pass.txt # Move vpn user from gitlab variable to pass file.
- cat <<< $VPN_P >> /etc/openvpn/pass.txt # Move vpn password from gitlab variable to pass file.
- cat <<< "auth-user-pass /etc/openvpn/pass.txt" >> /etc/openvpn/client.conf # Tell vpn config to use password file.
- cat <<< "log /etc/openvpn/client.log" >> /etc/openvpn/client.conf # Tell vpn config to use log file.
- openvpn --config /etc/openvpn/client.conf --daemon # Start openvpn with config as a deamon.
- sleep 30s # Wait for some time so the vpn can connect before doing anything else.
- cat /etc/openvpn/client.log # Print the vpn log.
- ping -c 1 <IP> # Ping the server I want to deploy to. If not available this stops the deployment process.
After this, you can add an after_script section to stop the OpenVPN daemon, or using a special closing job which includes a when: always, to ensure the VPN connection is closed even if the build failed.
You can also try other solutions, depending on your environment.

Nodemon not reloading page

I am starting studying Node.js, so I am using nodemon to reload my page, but it's not working and already tried all Stack solutions aswell.
Look how simple is my code:
package.json
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
},
"devDependencies": {
"nodemon": "^1.18.3"
}
}
server.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hey');
});
app.listen(3001);
My bash while interacting with res.send() message.
You are using nodemon with the server. It is restarting the server as you make changes to the server.js file. That is, your endpoints are being updated. This will not cause your client to reload. As you are simply navigating to the endpoint that you are creating within the browser, you will not see the changes reflected without refreshing.
That isn't to say that there is no benefit to running nodemon in this way. If you were not doing so you would need to also close the node instance (ctrl-c) and then rerun it every time before refreshing the page. Otherwise, you would still be running the old version of your server and still see the same content served.
Eventually you will consume these endpoints using an http client from your client application, this is generally when you take advantage of a hot reloading environment. There are some options here if you want to make express live-reload before then.

How do you hide the "[nodemon] clean exit - waiting for changes before restart" message?

I don't want to see that message whenever I save a file in my project. I already have
{
"events": {
"start": "node -e 'console.clear()'"
}
}
in my nodemon.json to indicate my project has restarted.
You can tell nodemon to be quiet, by passing the -q argument. According to nodemon --help options, this will:
minimise nodemon messages to start/stop only
Usage: nodemon -q

Categories

Resources