How to deploy node.js application in cyberpanel? - javascript

I have my application developed in node.js, and I have cyberpanel installed on my server. I have seen many examples of how to deploy a node application in cyberpanel, but I have doubts about how to view it from the browser.
So far I have the following configuration in vHost:
context / {
type appserver
location /FOLDER/FOLDER/PROJECT_FOLDER/dist
binPath /usr/bin/node
startupFile index.js
appType node
maxConns 100
}
My application runs perfectly on port 3000 when I run it by console, but I need to list it on port 80 with cyberpanel.
Does anyone have an idea how to do it?

try the following steps. Essentially, the error lies in selecting the root document folder and allowing access to the application.
Create a Website using the normal CyperPanel menu. [https://cyberpanel.net/docs/2-creating-website/]
Upload your Node.Js files into the public_html folder of the website.
Enter the Open Lite Speed panel via port :7080 (you would need to enable the port on the firewall)
Navigate to VH Hosts > Your Domain > Context
Select App Server, for location using $VH ROOT instead of the hardcoded path worked.
Additionally, don't forget to enable the site on access control via allowing all IPs (*).
context / {
type appserver
location $VH_ROOT/public_html/
binPath /usr/bin/node
appType node
startupFile server.js //this is the name of your
appserverEnv 1
maxConns 100
accessControl {
allow *
}
rewrite {
}
ad

See I am going to answer point to point to the question
First of all cyberpanel by default only takes app.js file as its core file to run the application.
Second, How to change that default file pointing ?
context / {
type appserver
startupFile index.js // **NAME OF YOUR STARTUP FILE**
location /home/PROJECT_FOLDER/public_html/dist
binPath /usr/bin/node
appType node
appserverEnv 1
maxConns 100
accessControl {
allow *
}
rewrite {
}
ad
location /FOLDER/FOLDER/PROJECT_FOLDER/dist
Note :- Things, I want to mention about this location parameter is this is the location to the your startup file, you will get it via file manager, as you cannot run typescript code directly here, you have to convert it into javascript using tsc command and further target dist folder using location parameter in vconfig file
Now next question is how to run application outside console ?
Create a website to deploy the project, use below link for reference click here
Issuing SSL for website - link for reference
This is my folder structure for deployment, simply zip all files and upload it on file manager of cyber panel, and extract out your files. You can see, I have dist folder which contains all javascript files and also have index.js, the main startup file.
Click on fix permissions on file manager.
Go to Web terminal and install node modules. how ?
on web terminal :- type cd .. and press enter.
There you have to find out your project from directory, You can use ls command to get list of files and folder structure.
mine directory was (after using cd ..) :- cd home/FOLDERNAME/public_html
At last run your project through terminal, to check its working.
Config your vhost config file, below is reference image
File you have to add in vhost config, I also had provided you above.
If you domain is setup correctly, you can view on api on your domain else you can click on preview button on cyber panel
Note :- Always Run code in terminal first to check its working.

Related

Generated next js chunks does not load on local environment

I recently joined a project written with Next Js and Squidex CMS.
The problem is I am not able to run it (properly) on my local environment.
The chunks are not loading and the images are not visible
I tried 2 ways
1 - Running the project with the same scripts on production pipeline
- clean `node_modules` and `.next` directories
- run `yarn build`
- run `yarn start`
The build is always successful but when I try to run the build by yarn start it does not load some js chunks and assets (images in static folder)
They are throwing 404 error on the console (please see the image)
I compared the hashes and they are fine.
My next config file is pretty empty.
const optimizedImages = require("next-optimized-images")
const withPlugins = require("next-compose-plugins")
module.exports = withPlugins([
[optimizedImages, {
responsive: {
adapter: require('responsive-loader/sharp')
}
}]
],
)
I also tried with multiple next versions (9.5.0 and 10.1.3)
The issue was the directory name of the project.
Repository named like 'Some Project' and when you try to clone it via SSH provided from repository website, the directory on your computer will be like this : 'Some%20Project'
Example ssh command :
git#ssh.dev.provider.com:Some%20Project
In that case, node.js server able to start and there is no error on the console unless you launch your browser and try to react the localhost. Node.js is not able to serve files from the directory because of not supported string chars like %
This cost me a lot of hours to find out, hope it will help

CORS policy error on file in same folder as HTML [duplicate]

I am getting the following error:
XMLHttpRequest cannot load file:///C:/Users/richa.agiwal/Desktop/get/rm_Library/templates/template_viewSettings.html. Cross origin requests are only supported for HTTP.
I realize that this question has been answered before, but I still have not found a solution to my problem. I tried running chrome.exe --allow-file-access-from-files from the command prompt, and moved the file to the local file system, but I still get the same error.
I appreciate any suggestions!
If you are doing something like writing HTML and Javascript in a code editor on your personal computer, and testing the output in your browser, you will probably get error messages about Cross Origin Requests. Your browser will render HTML and run Javascript, jQuery, angularJs in your browser without needing a server set up. But many web browsers are programed to watch for cross site attacks, and will block requests. You don't want just anyone being able to read your hard drive from your web browser. You can create a fully functioning web page using Notepad++ that will run Javascript, and frameworks like jQuery and angularJs; and test everything just by using the Notepad++ menu item, RUN, LAUNCH IN FIREFOX. That's a nice, easy way to start creating a web page, but when you start creating anything more than layout, css and simple page navigation, you need a local server set up on your machine.
Here are some options that I use.
Test your web page locally on Firefox, then deploy to your host.
or: Run a local server
Test on Firefox, Deploy to Host
Firefox currently allows Cross Origin Requests from files served from your hard drive
Your web hosting site will allow requests to files in folders as configured by the manifest file
Run a Local Server
Run a server on your computer, like Apache or Python
Python isn't a server, but it will run a simple server
Run a Local Server with Python
Get your IP address:
On Windows: Open up the 'Command Prompt'. All Programs, Accessories, Command Prompt
I always run the Command Prompt as Administrator. Right click the Command Prompt menu item and look for Run As Administrator
Type the command: ipconfig and hit Enter.
Look for: IPv4 Address . . . . . . . . 12.123.123.00
There are websites that will also display your IP address
If you don't have Python, download and install it.
Using the 'Command Prompt' you must go to the folder where the files are that you want to serve as a webpage.
If you need to get back to the C:\ Root directory - type cd/
type cd Drive:\Folder\Folder\etc to get to the folder where your .Html file is (or php, etc)
Check the path. type: path at the command prompt. You must see the path to the folder where python is located. For example, if python is in C:\Python27, then you must see that address in the paths that are listed.
If the path to the Python directory is not in the path, you must set the path. type: help path and hit Enter. You will see help for path.
Type something like: path c:\python27 %path%
%path% keeps all your current paths. You don't want to wipe out all your current paths, just add a new path.
Create the new path FROM the folder where you want to serve the files.
Start the Python Server: Type: python -m SimpleHTTPServer port Where 'port' is the number of the port you want, for example python -m SimpleHTTPServer 1337
If you leave the port empty, it defaults to port 8000
If the Python server starts successfully, you will see a msg.
Run You Web Application Locally
Open a browser
In the address line type: http://your IP address:port
http://xxx.xxx.x.x:1337 or http://xx.xxx.xxx.xx:8000 for the default
If the server is working, you will see a list of your files in the browser
Click the file you want to serve, and it should display.
More advanced solutions
Install a code editor, web server, and other services that are integrated.
You can install Apache, PHP, Python, SQL, Debuggers etc. all separately on your machine, and then spend lots of time trying to figure out how to make them all work together, or look for a solution that combines all those things.
I like using XAMPP with NetBeans IDE. You can also install WAMP which provides a User Interface for managing and integrating Apache and other services.
Simple Solution
If you are working with pure html/js/css files.
Install this small server(link) app in chrome. Open the app and point the file location to your project directory.
Goto the url shown in the app.
Edit: Smarter solution using Gulp
Step 1: To install Gulp. Run following command in your terminal.
npm install gulp-cli -g
npm install gulp -D
Step 2: Inside your project directory create a file named gulpfile.js. Copy the following content inside it.
var gulp = require('gulp');
var bs = require('browser-sync').create();
gulp.task('serve', [], () => {
bs.init({
server: {
baseDir: "./",
},
port: 5000,
reloadOnRestart: true,
browser: "google chrome"
});
gulp.watch('./**/*', ['', bs.reload]);
});
Step 3: Install browser sync gulp plugin. Inside the same directory where gulpfile.js is present, run the following command
npm install browser-sync gulp --save-dev
Step 4: Start the server. Inside the same directory where gulpfile.js is present, run the following command
gulp serve
To add to Alan Wells's elaborate answer here is a quick fix
Run a Local Server
you can serve any folder in your computer with Serve
First, navigate using the command line into the folder you'd like to serve.
Then
npx i -g serve
serve
or if you'd like to test Serve without downloading it
npx serve
and that's it! You can view your files at http://localhost:5000
If you are using vscode, you can easily start a liver server. Click liver server at the bottom of the page, once the server is started, vscode will tell the port the project is running. Do ensure your project folder is the workspace
This error is happening because you are just opening html documents directly from the browser. To fix this you will need to serve your code from a webserver and access it on localhost. If you have Apache setup, use it to serve your files. Some IDE's have built in web servers, like JetBrains IDE's, Eclipse...
If you have Node.Js setup then you can use http-server. Just run npm install http-server -g and you will be able to use it in terminal like http-server C:\location\to\app.
Kirill Fuchs
If you use the WebStorm Javascript IDE, you can just open your project from WebStorm in your browser. WebStorm will automatically start a server and you won't get any of these errors anymore, because you are now accessing the files with the allowed/supported protocols (HTTP).
I was facing this error while I deployed my Web API project locally and I was calling API project only with this URL given below:
localhost//myAPIProject
Since the error message says it is not http:// then I changed the URL and put a prefix http as given below and the error was gone.
http://localhost//myAPIProject
Depends on your needs, but there is also a quick way to temporarily check your (dummy) JSON by saving your JSON on http://myjson.com. Copy the api link and paste that into your javascript code. Viola! When you want to deploy the codes, you must not forget to change that url in your codes!

Building docker image task results in VSTS error

I'm following
this Microsoft tutorial on how to containerize and deploy an Angular application on Azure using Docker.
I'm running into a unique issue that is not addressed in the tutorial. Every time the build reaches the build an image task it fails with the following error.
2018-01-29T12:37:28.3169577Z [command]/usr/local/bin/docker build -f
/opt/vsts/work/1/s/Dockerfile -t iponam.azurecr.io/my-angular-app:197
/opt/vsts/work/1/s
2018-01-29T12:37:28.7630517Z Sending build context to Docker daemon
593.4 kB
2018-01-29T12:37:28.7639048Z
2018-01-29T12:37:28.7859042Z Step 1/3 : FROM nginx
2018-01-29T12:37:30.3172506Z latest: Pulling from library/nginx
2018-01-29T12:37:30.3188560Z e7bb522d92ff: Pulling fs layer
2018-01-29T12:37:30.3204163Z 6edc05228666: Pulling fs layer
2018-01-29T12:37:30.3219390Z cd866a17e81f: Pulling fs layer
2018-01-29T12:37:30.3279280Z cd866a17e81f: Download complete
2018-01-29T12:37:30.3294450Z e7bb522d92ff: Verifying Checksum
2018-01-29T12:37:30.3310189Z e7bb522d92ff: Download complete
2018-01-29T12:37:30.3511653Z 6edc05228666: Verifying Checksum
2018-01-29T12:37:30.3529571Z 6edc05228666: Download complete
2018-01-29T12:37:32.1518557Z e7bb522d92ff: Pull complete
2018-01-29T12:37:33.4982718Z 6edc05228666: Pull complete
2018-01-29T12:37:33.6163338Z cd866a17e81f: Pull complete
2018-01-29T12:37:33.6365600Z Digest:
sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
2018-01-29T12:37:33.6540818Z Status: Downloaded newer image for
nginx:latest
2018-01-29T12:37:33.6558500Z ---> 3f8a4339aadd
2018-01-29T12:37:33.6577887Z Step 2/3 : COPY dist /usr/share/nginx/html
2018-01-29T12:37:33.6595536Z COPY failed: stat
/var/lib/docker/tmp/docker-builder545265281/dist: no such file or
directory
2018-01-29T12:37:33.6780204Z ##[error]COPY failed: stat
/var/lib/docker/tmp/docker-builder545265281/dist: no such file or
directory
2018-01-29T12:37:33.6855716Z ##[error]/usr/local/bin/docker failed with
return code: 1
Per the tutorial, this is running on a hosted linux agent that I cannot access to examine folder structure. Essentially what's happening is my Dockerfile (below) is pulling an nginx image, building the angular application with ng build prod and then I try to COPY the dist folder into the /usr/share/nginx/html. Here's the Dockerfile, it's identical to the one in the tutorial as well:
FROM nginx
COPY dist /usr/share/nginx/html
EXPOSE 80
I have spent countless hours trying to debug and understand the issue. Any help will be tremendously appreciated. Thank you!
Update
After run build is executed successfully I don't see where the dist directory is outputted to. I checked every single place on the machine with no luck. On my local machine the dist will appear in the root of the project folder.
Using this command instead in NPM build task (Command and arguments):
run build -- -op dist
okay, first off all, you can examine the folder structure, you can add a Shell script step before docker build and do something like ls -Rla, which is a hack, undoubtebly, but a working one.
As for you error, it indicates that there is no such file (dist) under the working directory. Again, what you could do is verify this file is in the root of the repo or use the ls hack to locate the file. You should also check that you didnt change the default working directory of the docker build step. If you did you need to specify path to dist file relative to the working directory.

Simplest Way To Deploy Javascript Application On Linux

I have an folder say 'mywebapp' on windows machines. This folder has index.html page, js directory with java script files and css directory with css files.
Now when i open this index.html into browser, the browser displays contents pretty well, as if i have deployed this application on server, which is not the case.
Now i wanted to do same on my Linux machine vm, login-ed through putty. I tried using pythons SimpleHTTPServer which gave me same result. But as soon as i exit from putty session, the webpage doesnt display. seems like SimpleHTTPServer server connection is broken once i exit the putty session.
Please help me.
Or any other professional and easy way to get my webpage displayed. Tomcat seems good option but i don't have root permission and don't want hectic deployment process.
I heard about node.js, but i don't have root permission to install node.
Most simplest way i can suggest it to download and copy tar.gz file from location:
https://tomcat.apache.org/download-70.cgi
1 then gunzip and untar this downloaded file.
2 Go to conf/Catelina/localhost folder.
3 create an xml with your application name, e.g. mywebapp.xml
and put following to this file:
<Context path="/mywebapp" reloadable="false" docBase="<root-path of your application folder>"/>
here "root-path of your application folder" will be the root folder of ypur HTML, js and css files.
Then just start this tomcat using /bin/startup.sh command and check on browser using localhost:8080/mywebapp

Heroku(Cedar) + Node + Express + Jade Client-side javascript files in subdirectory work locally with foreman+curl but not when pushed to Heroku

I am very new to node and heroku and I suspect this is some kind of simple permission issue etc, but I can't seem to track it down.
I have several pure javascript files in a sub-directory one level beneath my root directory where my web.js file is sitting. I have a line in my web.js file to specify the directory
app.use('/heatcanvas',express.static(__dirname+'/heatcanvas'));
If I run my app locally with Heroku Foreman I get the expected js response when I run the following curl command
curl localhost:5000/heatcanvas/heatcanvas.js
However when I push to Heroku and hit the corresponsing live url in the browser
www.example.com/heatcanvas/heatcanvas.js
I receive the following:
Cannot GET /heatcanvas/heatcanvas.js
If I check Firebug and/or the Heroku logs I see I am actually getting 404 errors for those files even though the pathing should match what is being done locally. It is also worth mentioning that third party javascript is coming over just fine, it is only when the src attribute of the script tag points to my site that there is an issue. What do I need to do to get my scripts to be available?
I recommend you to use process.cwd() value to get specific directory
process.env.PWD = process.cwd()
at the very beginning of your web.js
let you access files easily.
You can do
app.use('/heatcanvas',express.static(process.env.PWD+'/heatcanvas'));
instead of using
__dirname
Warning: Make sure to execute web.js at the root directory of web.js (Heroku web.js are executed that way)

Categories