How to set automatically call "npm start" in nodejs? - node.js

I am using nodejs for web application. In this I am frustrated with continuously firing command "npm start" when applying small changes on any files.
Question 1 : Is there any way for how to keep automatically call my "npm start" without opening command prompt?
Question 2 : And also I want to remove this port display in url.
I want like below result, example:
Current url: http://localhost:3200/login
Expected url: http://localhost/myprojectname/login

node-dev will automatically restart your application whenever you save a code change. Install it with npm install -g node-dev then run your application with node-dev server.js (or whatever the entrypoint file for your project is called if not server.js).
For development, it's easiest to just live with the port in the URL. To get rid of it, you need to run your application on port 80 which requires running as root which is insecure. There are ways to do it (via ngrok iptables, nginx, etc, but most developers opt to just deal with the port in the URL during local development

Try nodemon module, Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.Perfect for development on local machine, you no longer need to deal with npm start every time when you change the code.It will automatically restart the server and changes will be reflect instantly.
install it globally
npm install -g nodemon
then try to run your app using nodemon app.js instead of npm start or node app.js
If you want to use port 80, you should run your application with root/administrator privileges, and make sure no other service is running on that port another solution is which I strongly recommend is to use nginx server reverse proxy settings .Install nginx server on your machine, Following are the settings that you can apply using nginx reverse proxy to remove any port number you want from the url.
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect http://localhost:3000/ https://$server_name/;
}
}
Hope this helps you.

Related

Running .NET Core 5.0 on Debian Nginx

I wanted to move a project I have working on Windows to run on a Unix machine.
The machine running on Debian 9 with Nginx.
This project runs absolutely fine on Windows with IIS.
I've followed all the instructions on here created a service for this to run on the start of the machine and Nginx configuration to proxy the connection from the port I want to use to port 5000.
When I start the application running Dotnet Myddl.dll it starts and says it is only listening on port 5000.
Then when I try to access it, I can see a warning.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the HTTPS port for redirect.
I know it is related to my app redirecting to HTTPS and not knowing where to redirect it, but how do I resolve this?
My service
[Unit]
Description=Myapp API
[Service]
WorkingDirectory=/var/www/myapp/publish
ExecStart=/usr/bin/dotnet /var/www/myapp/publish/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
server {
listen 6969;
server_name mysite.net *.mysite.net;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EDIT:
I've been trying to resolve this and still can't. When I start the app on the unix machine I get the following
root#myhost:/var/www/myapp/publish# dotnet Myapp.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /var/www/myapp/publish
Obviously it is missing https option, and I can't figure why.
EDIT2:
I've published the app as self contained for Linux-x64, and now I do not get the warning saying that it couldn't determine the https port, on my browser I get redirected to https://mydomain:5001 when I access it on http://mydomain:6969
Still I do not get the app listening on https on Unix, just on Windows.
EDIT3:
Noticed that if I go to one of my endpoints e.g. http://IP:6969/api/users I get a 500 response.
EDIT4:
When I was loading my application locally, I was getting straight through to the swagger page, such as /swagger/index.html, for some reason my API when complied for Linux does not accept this URL and returns me a 404, but if I get to one of my endpoints e.g. /api/users, it does return me the data I was expecting for.
Default https port should be 5001.
You can set https port following offical docs.
Or disable relatied middlware. Use nginx for https termination if needed.
I was trying to Migrate an API, and as on Windows it was returning me to my swagger page located on /swagger/index.html I was expecting the same to happen, which for some reason, doesn't.
So if I access one of my endpoints (e.g. /api/users) it does work fine.

ReactDOM not rendering anything from separate js file

I used "npx create-react-app testreact" to create a react project. When testing from browser on my PC, the server returns a blank page, other than the expected words —— "Learn React".
What's happening here? how to fix it?
Cmd Sequnce
npx create-react-app testreact
npm install
npm start
Env Info
nodejs: v13.2.0
npm: 6.13.1
system: 18.04.1-Ubuntu
reverse proxy server: nginx
Code Info
repo: https://github.com/nautilusshell/testreact.git
there is folder called node_modules which was created by "npm install", I didn't put into above repo because it is too big and not necessary to upload.
by the way, actually I didn't change anything.
Err Info
visit http://52.130.83.55/image_editing
from browser I got:
the .js files whose status equal 404 all locates at http://52.130.83.55/static/js/xxx.js. This path was told by my chrome browser, but I searched my server, by command
find / -name "0.chunk.js" 2>/dev/null, only to find that there are no such files.
For example, http://52.130.83.55/static/js/0.chunk.js was not found anywhere on my server.
Nginx Config
repo: https://github.com/nautilusshell/nginx_config.git
Your nginx configuration has the following block:
location /image_editing {
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 1000M;
proxy_pass http://image_editing;
}
This block presumably points anyone who visits [hostname]/image_editing is shown your React App, however it does not rewrite the path. This means that if you visit localhost/image_editing, the path is still /image_editing in the browser.
Any paths to assets, such as /static/js/bundle.js (note the leading slash), which is used by default by CRA, will not go to the webserver hosted by Create React App but instead will hit Nginx itself. You have no configuration to forward requests to /static to the same server as /image_editing, so all of those requests will fail.
In other words, when you visit yourdomain/image_editing, the index.html of your React app is sent back declaring that all of the assets are located at http://yourdomain/static. This request hits Nginx and Nginx correctly serves 404 because it does not know about those assets. You need to configure your React App such that it 'thinks' that the assets are actually located at http://yourdomain/image_editing/static/.
To solve this, you need to modify the publicPath property of the webpack configuration of your project. Unfortunately, this will require ejecting your Create React App configuration using npm eject.
You could also resolve this on the Nginx side using subdomains. For example, you could host your image editing app at image-editing.yourdomain.com and all of the URLs with leading slashes would resolve correctly.

Nginx - Load balancing returns 404 on Windows

I have installed Nginx for Windows (64-bit) from here because the official binaries are 32-bit. The aim is to use Nginx for load balancing NodeJS applications. I am following instructions from here where, the link to sample basic configuration file also exists.
The following configuration file works successfully on Linux where nginx was installed via Ubuntu PPA. The servers are themselves started via pm2.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream top_servers {
# Use IP Hash for session persistence
ip_hash;
# Least connected algorithm
least_conn;
# List of Node.JS Application Servers
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
server 127.0.0.1:3004;
}
server {
listen 80;
server_name ip.address;
location /topserver/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://top_servers;
proxy_set_header X-Request-Id $request_id;
}
}
However, this file does not work with Windows. I am getting an error as 'No such file or directory' under the html folder of Nginx installation on Windows. I haven't done any such setting for Linux.
Can you please help me convert the above configuration file for Windows?
NOTE I don't have a choice - Windows is a must for this project.
So, I over-wrote the contents of conf/nginx.conf with the contents shown above. First, I got an error as "map" directive is not allowed here. Then, after removing this directive, I got another error as "upstream" directive is not allowed here". I think, the binaries I am using does not support load balancing.

Nginx & Node inside the same Docker container

I know that this is against the accepted convention but I need to run Nginx alongside a node.js server inside the same Docker container. I have no issues spinning up the container and getting Nginx and Node working alongside. Nginx works on Port 443 which is exposed by the container. The Node server listens on Port 8080 and is reverse proxied by Nginx
location /node/index.js {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X_Forwarded_For $proxy_add_x_forwarded_for;
}
Here is what I find
Access to the resources, https://example.com/text.txt on the Nginx server is straightforward
I startup the Nodejs server nodejs index.js & disown which has been configured to output a few diagnostic messages to a log file. Those messages tell me that the server is working just fine.
I can Telnet to Port 8080 from inside the Docker container - once again indicating that the Node server i sup and running
Precisely the same configuration but with Nginx running on a "real" server with Node running on the same server offers acceess to the Node server with no issues.
However, when I attempt to access the Node server running inside the Nginx Docker container, say, https://example.com/node/index.js I get a 404 error.
Examining my Nginx logs reveals that the request did reach Nginx inside its Docker host. However, examining the Node server log file indicates that the request never got forwarded.
It is not clear to me why this could be happening. From what I can tell when Nginx is running inside a Docker container it is failing to act as a reverse proxy for Node running inside the same container.
For good measure I tried EXPOSing the Node port, 8080 and starting up the Docker container with -p 8080:8080 -p 443:443 but that made no difference. I'd be most grateful to anyone who might be able to shed any light on what is going on here.

Install Ghost Blog

I've been trying to get Ghost.io installed on my web server for quite sometime. I have a VPS with Centos 6 and Cpanel.
Today I found a script at http://www.allaboutghost.com/one-click-ghost-install-script/ that said you could just enter a command into your ssh terminal and have it all installed for you.
Command
wget -O - https://raw.github.com/howtoinstallghost/installghost.sh/master/installGhost.sh | sudo bash
I did this and it appears to have worked, I didn't get any errors but now I am not able to find the install in either FileZilla or by using my web browser. The website says that it installs in the /var/www/ghost/ directory but I can't find that. If I use cd /var/www/ghost/ in the ssh it takes me right to it and even lets me edit the config.example.js file.
If I direct my browser to www.mydomain.com:80 since the site says it installs on port 80 it just takes me back to my home page.
What am I missing and what do I need to do?
As Per the comments, I did follow the instructions on the github page. Now All I get when I visit mydomainname.com/ghost/
Ghost is installed really easy. You'd better don't use 3rd party scripts for that as it might really vary from system to system. All you need is to get Node.js installed and then follow instruction for example from here: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ghost-on-ubuntu-16-04. They are very detailed and must work for Centos as well.
Most common errors are:
- Not configuring a reverse proxy (nginx or apache) to link to your ghost install on port 2368. Here is an example for Nginx:
server {
listen 80;
server_name your_domain_or_ip_address;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:2368;
}
}
If you now see default home page it means some web server is already running and intercept all the requests and parks them to a default location (e.g. /var/www)
- If you want Ghost to be the one and the only web server on your VPS, you have to remove or shut down currently installed web server and try to configure ghost to answer on port 80 like this:
server: {
host: '0.0.0.0',
port: '80'
}
I didn't test it but has to work. This kind of install is not recommended and insecure. I suppose you can configure reverse proxy from Cpanel, but not sure.
The best and easiest way to set up Ghost is using SSH.
Hope it helps. For any further help, you have to provide more details and possibly logs and configs. Most of the possible errors you'll catch when installing or starting your blog with npm
sudo npm install --production
sudo npm start --production
Good luck with your deployment.
Hey you don't need to do that much you can just launch ghost from digitalpress for free...
Know how to host ghost blog for free out here : https://treanches.digitalpress.blog/hosting-ghost-blog/
You can think this as self promotion but this article is so much understanding you won't regret reading it

Resources