If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Node js server on webhosting

Started by meganiams, Jan 30, 2023, 09:13 AM

Previous topic - Next topic

meganiamsTopic starter

My project is built upon Angular with a server running on Node.js. While I want to host the project, I'm clueless about how I can start the server. Currently, I have to run it via the "node server.ts" command in a separate terminal window.
Can you guide me on how to get the server running successfully on the hosting platform?
  •  


diyasaini

You can rent a server and install Ubuntu, Node, Nginx, and PM2 to run your script. Then configure Nginx to proxy requests from your domain to the Node application. Finally, fill in the project files, run 'npm i', and start the server.

There are tutorials available on Google, and the process can be completed in roughly half an hour. DigitalOcean offers excellent guides for setting everything up and allows you to rent a server there as well.
  •  

Lechlak


It is advisable to use "forever" to execute node.js scripts on a server. By using this package, the process will continue even after closing the console window, as it runs as a separate daemon.
Additionally, "forever" also restarts the script in case of a crash, and it comes with a default setting of five startup attempts at an interval of one second. However, these settings can be modified to meet specific requirements. You can access "forever" at https://www.npmjs.com/package/forever.
  •  

Mycrib

To install Node.js on a server, we recommend following these steps. First, to start using the required Node version (assuming SSH access has been granted), run the following commands in the terminal to download and extract the file archive:

wget "path to node archive.js"
tar xfz node-file.tar.gz

Ensure that you download the 64-bit version for Linux, and that the archive has a .tar.gz extension as shown above.

In order for Passenger to interact with Node.js, you need to modify the .htaccess file. We suggest placing this file at "./site-name.com/htdocs/www/.htaccess" and adding these lines:

PassengerAppRoot /home/hostxхxxхxX/site-name.com/nodejs_app/www
PassengerAppType node
PassengerStartupFile app.js
PassengerNodejs /home/hostxхxxхxX/node/bin/node
PassengerRestartDir /home/hostxхxxхxX/site-name.com/nodejs_tmp/www
PassengerFriendlyErrorPages on

Replace "hostxхxxхxX" with your account number, and "site-name.com" with the folder name where your project resides. "app.js" should be the name of the file that starts the server part, but you can name it whatever you prefer. "PassengerNodejs" refers to the path to the Node executable file. It is also recommended to enable "PassengerFriendlyErrorPages" only during the development stage.

If you encounter any errors, you can check the logs folder located in the root of the site. Make sure that the option is enabled in the control panel under "Domains" → "Domain attachment". If needed, you can forcibly terminate all processes using the command "killall -2 'Passenger NodeApp'", followed by a page reload.

Lastly, add the code for running Node in app.js. Below is an example code from the official Node website:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
});

server.listen(port, hostname, () => {
    console.log('Server running at http://${hostname}:${port}/');
});

After following these steps, open your site in a new tab. If "Hello World" is displayed, then everything should be working correctly. If not, review the instructions above to ensure you didn't miss anything.
  •  

faseeo

You should choose the right hosting platform. For a Node.js server, platforms like Heroku, DigitalOcean, or Vercel are popular. Let's take Heroku as an example since it's user-friendly and great for beginners.

1. Create an account on Heroku: Go to heroku.com and sign up for a free account if you haven't already.

2. Install Heroku CLI: Download and install the Heroku Command Line Interface. This lets you interact with your Heroku apps directly from the terminal.

3. Prepare your project: Ensure your Node.js server is ready for deployment. You must have a package.json file in your project, and it should contain a start script. It should look something like this:

{
"scripts": {
"start": "node server.js"
}
}

Make sure it's using the correct entry file name, whether it's server.js, server.ts, etc.

4. Create a Procfile: This file tells Heroku how to run your application. Create a file named Procfile in your root directory (without any extension) and add this line:

web: node server.js

5. Initialize a Git Repository: If you haven't already done so, initialize a git repository in your project folder:

git init

6. Login to Heroku: Open your terminal and type:

heroku login

This will open a browser window where you can log in.

7. Create a New Heroku App: Run the following command to create your app on Heroku:

heroku create your-app-name

Replace your-app-name with a name of your choosing.

8. Deploy Your App: Add your changes to git and deploy your application:

git add .
git commit -m "Initial commit"
git push heroku master

9. Set Node Environment: Sometimes, you'll want to set your environment to production. You can do that by running:

heroku config:set NODE_ENV=production

10. Open Your App: Once the push is complete, you can open your app by running:

heroku open

11. Check Logs: If any issues arise, you can view the logs to debug with:

heroku logs --tail

Now you should have your project up and running on Heroku! If you need more specific information based on issues you encounter, feel free to ask.
  •  

TahInaria

To get your Node.js server running smoothly on a hosting platform, you'll need to explore options beyond manual execution via the terminal. Consider using a process manager like PM2 (Process Manager 2) or Forever.js, which can automatically restart your server in case of crashes. These tools can be easily integrated with most hosting platforms.
Additionally, you can leverage Docker containers to encapsulate your server environment, ensuring seamless deployment and scaling.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...