{"id":1,"date":"2021-01-12T03:48:50","date_gmt":"2021-01-12T03:48:50","guid":{"rendered":"http:\/\/www.maginobi.net\/main\/?p=1"},"modified":"2021-01-23T19:56:47","modified_gmt":"2021-01-23T19:56:47","slug":"node-js-cpanel","status":"publish","type":"post","link":"http:\/\/leif.sahyun.net\/main\/?p=1","title":{"rendered":"Tips For Hosting Node.js Apps With cPanel"},"content":{"rendered":"\n<h4 class=\"pagelayer-heading-holder wp-block-heading\">Setup Guide<\/h4>\n\n\n\n<p class=\"pagelayer-text\">You can setup Node.js apps through cPanel by going to the &#8220;Setup Node.js App&#8221; button, clicking &#8220;Create Application&#8221; and filling out the details there, including application root, url, and startup file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"241\" src=\"http:\/\/www.maginobi.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_setup_njs.jpg\" alt=\"\" class=\"wp-image-60\" srcset=\"http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_setup_njs.jpg 383w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_setup_njs-300x189.jpg 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"428\" src=\"http:\/\/www.maginobi.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_njs_fields-1024x428.jpg\" alt=\"\" class=\"wp-image-61\" srcset=\"http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_njs_fields-1024x428.jpg 1024w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_njs_fields-300x125.jpg 300w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_njs_fields-768x321.jpg 768w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_njs_fields.jpg 1434w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"pagelayer-text wp-block-list\"><li>The Application Root tells the server where your application code is stored.<\/li><li>The&nbsp;Application Url&nbsp;determines what public url your application will be accessible from; more information below.<\/li><li>The&nbsp;Application Startup File&nbsp;is the name of the entry point Javascript file for your application. This is the same &#8216;main&#8217; file referenced in your package.json file if you have one.<\/li><\/ul>\n\n\n\n<p class=\"pagelayer-text\">Click the &#8220;Run NPM Install&#8221; button to install your application&#8217;s dependencies. Then you can click the &#8220;Start App&#8221; button.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Application Url<\/h4>\n\n\n\n<p>The application url that you specify during application setup does not by itself determine what urls you can access your application from. All that the application url specified during setup does is create the appropriate path in your public html directory and adds a .htaccess file at that path, which directs any http requests to that path over to your application. If your application does not have logic based on the request path, this will allow your application to be accessed at the url you specified. For example, something like the following code snippet from <a href=\"https:\/\/docs.cpanel.net\/knowledge-base\/web-services\/how-to-install-a-node.js-application\/\">cPanel&#8217;s Node.js setup example<\/a> would work great.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const http = require('http');<\/code>\n<code>const hostname = '127.0.0.1';<\/code>\n<code>const port = 3000;<\/code>\n<code><\/code>\n<code>const server = http.createServer((req, res) =&gt; {<\/code>\n<code>  res.statusCode = 200;<\/code>\n<code>  res.setHeader('Content-Type', 'text\/plain');<\/code>\n<code>  res.end('Hello World! NodeJS \\n');<\/code>\n<code>});<\/code>\n<code><\/code>\n<code>server.listen(port, hostname, () =&gt; {<\/code>\n<code>  console.log(`Server running at http:\/\/${hostname}:${port}\/`);<\/code>\n<code>});<\/code><\/pre>\n\n\n\n<p>However, many (most) applications provide multiple services at different paths with some routing logic based on the request path. For these applications, Phusion Passenger does not clip the base application url from the request before passing the request to your application, nor does it pass the base application url to Node.js through an environment variable or some other means. So, if your application uses routing like in the following code snippet with express, it will not work:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const express = require('express');<\/code>\n<code>const hostname = '127.0.0.1';<\/code>\n<code>const port = 3000;<\/code>\n<code><\/code>\n<code>const app = express();<\/code>\n<code><\/code>\n<code>app.get('\/hello', (req, res) =&gt; {<\/code>\n<code>  res.send('Hello Route! Express NodeJS');<\/code>\n<code>});<\/code>\n<code><\/code>\n<code>app.listen(port, hostname, () =&gt; {<\/code>\n<code>  console.log(`Server running at http:\/\/${hostname}:${port}\/`);<\/code>\n<code>});<\/code><\/pre>\n\n\n\n<p>This can be fixed by adding a BASE_URL environment variable to pass in the configured application url (more on adding environment variables below) and stripping that string off the request url before the rest of your routing takes place.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const base_url = process.env.BASE_URL || '';<\/code>\n<code><\/code>\n<code>const app = express();<\/code>\n<code>const someRoute = express.Router();<\/code>\n<code>\/\/ Set up someRoute...<\/code>\n<code><\/code>\n<code>app.use(base_url, someRoute);<\/code><\/pre>\n\n\n\n<p>With our hello world example, that would give us&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const base_url = process.env.BASE_URL || '';<\/code>\n<code>const express = require('express');<\/code>\n<code>const hostname = '127.0.0.1';<\/code>\n<code>const port = 3000;<\/code>\n<code><\/code>\n<code>const app = express();<\/code>\n<code>const route = express.Route();<\/code>\n<code><\/code>\n<code>route.get('\/hello', (req, res) =&gt; {<\/code>\n<code>  res.send('Hello Route! Express NodeJS');<\/code>\n<code>});<\/code>\n<code><\/code>\n<code>app.use(base_url, route);<\/code>\n<code>app.listen(port, hostname, () =&gt; {<\/code>\n<code>  console.log(`Server running at http:\/\/${hostname}:${port}${base_url}\/`);<\/code>\n<code>});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Environment Variables<\/h4>\n\n\n\n<p>cPanel allows you to set the environment variables under which your application will be run using the application setup interface. These environment variables can be accessed from within the Node.js code as properties of process.env. The following images show an example setup for a BASE_URL environment variable.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"132\" src=\"http:\/\/www.maginobi.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_add_env_var-1024x132.jpg\" alt=\"\" class=\"wp-image-58\" srcset=\"http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_add_env_var-1024x132.jpg 1024w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_add_env_var-300x39.jpg 300w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_add_env_var-768x99.jpg 768w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_add_env_var.jpg 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"147\" src=\"http:\/\/www.maginobi.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_base_url_var-1024x147.jpg\" alt=\"\" class=\"wp-image-59\" srcset=\"http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_base_url_var-1024x147.jpg 1024w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_base_url_var-300x43.jpg 300w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_base_url_var-768x110.jpg 768w, http:\/\/leif.sahyun.net\/main\/wp-content\/uploads\/2021\/01\/cpanel_base_url_var.jpg 1395w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>A special environment variable here is the NODE_ENV application mode variable. It will always be one of the lowercase strings &#8216;development&#8217; or &#8216;production&#8217; and it is controlled separately from other environment variables by using the &#8220;Application mode&#8221; dropdown at the top of the setup menu.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Red Herrings<\/h4>\n\n\n\n<p>Some other websites offering tips about Node.js and cPanel point to port and .htaccess configuration as possible causes of issues that you might have setting up a Node.js app, but these are red herrings.<\/p>\n\n\n\n<p>Ordinarily, running multiple Node.js applications (or other applications) on the same port would create conflicts, leading to a need to carefully configure your application to avoid ports that are in use. However, Phusion Passenger uses <a href=\"https:\/\/www.phusionpassenger.com\/library\/indepth\/nodejs\/reverse_port_binding.html\">reverse port binding<\/a> for Node.js apps, which means that any port you specify to listen on in your application is ignored by Passenger and replaced with a random available port determined by Passenger. Then, http traffic on port 80 sent to the application url configured in application setup is redirected to your application&#8217;s listener, so you don&#8217;t need to worry about what ports are in use.<\/p>\n\n\n\n<p>The .htaccess file generated by Passenger when you create your application is responsible for redirecting said http traffic to Passenger so it can reach your application. The default .htaccess file works fine; there is no need to modify it. It is possible you could have problems with your application if you have customized or removed the .htaccess file, but you can get a new one by going to cPanel&#8217;s Node.js app setup and creating a new, empty application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setup Guide You can setup Node.js apps through cPanel by going to the &#8220;Setup Node.js App&#8221; button, clicking &#8220;Create Application&#8221; and filling out the details there, including application root, url, and startup file. The Application Root tells the server where your application code is stored. The&nbsp;Application Url&nbsp;determines what public url your application will be accessible [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[3],"class_list":["post-1","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-featured"],"_links":{"self":[{"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1"}],"version-history":[{"count":16,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":171,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions\/171"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=\/wp\/v2\/media\/170"}],"wp:attachment":[{"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leif.sahyun.net\/main\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}