How to Redirect NodeJS to Another Port


How to Redirect NodeJS to Another Port - Hi Everyone! Sometimes there are some NodeJS that cannot be accessed via a web browser. So, for that, make sure you have changed the .htaccess file so that the NodeJS file can be run. The way to redirect NodeJS to another port is like this guide.

Remember, the NodeJS feature is only available on Super Hosting Packages and above !

How to Run NodeJS Over Specific Ports?

Sometimes, in certain circumstances you have to run a Node.js script via a different port. By default, the port used in Node.js is port 8080. This is because Node.js functions as a server side that allows scripts / applications not to use a web server. Node.js to run through a particular port you can do the following things,
Buat file .js dengan script seperti di bawah ini. In part server.listen(), please isi dengan port yang anda pilih dan pastikan menggunakan port puluhan ribu, for example use port 62542. So, your script settings to server.listen (62542).

 

var http = require(‘http’);

var server = http.createServer(function(req, res) {

res.writeHead(200);

res.end(‘Hello Http’); });

server.listen(62542);

how to redirect nodeJS to another port

How to make Node.js service accessible using tcp port: 80 / http?

Js service so that it can be accessed on port http / tcp: 80 can be done in the following way,

1. Adding File. Htaccess

Add it file .htaccessfor the domain / subdomain / subfolder that wants to use the Node.js service, where the .htaccess rule is used to redirect to the Node.js service. please create a new file / click the file .htaccessthen select Code Editorat the top of the File Manager. Klik Edit.
how to redirect nodeJS to another port

 

After that, please tulis scriptfollow this.

Options + FollowSymLinks -Indexes
IndexIgnore *
DirectoryIndex
<IfModule mod_rewrite.c>
RewriteEngine on
# Simple URL redirect:
RewriteRule ^ (. *) $ Http://127.0.0.1:62542/$1 [P]
</IfModule>

Port 62542 merupakan port yg dipakai Node.js sebagai daemon. For example, for example, https://127.0.0.1:62542 . It matters anda dapat mengakses localhost melalui port 62542. Why should it be added to the .htaccess file? This is because port 62542 cannot be accessed directly via a web browser. So, you need to add rules to the .htaccess file so that it can be accessed via a web browser.
how to redirect nodeJS to another port

Conclusion

You have to add some rules in the .htacess file so that it can be accessed directly via a web browser. How to redirect NodeJS to another port is very easy. you just need to change the .htaccess file! If you have any questions, don't hesitate to contact the KincaiHosting Support Team ! You can also install Ghost on Hosting, you know! Please follow How to Install Ghost on Hosting . Do you want your NodeJS service to run continuously? Please read the Guide to Keep NodeJS Going On .

Published on: 3/2/21, 5:03 PM