How to start a node on the server ?
Introduction
[edit]Starting a node.js server on a remote server via SSH allows you to deploy and run web applications or services. This guide outlines the steps to initiate a node.js server on a server, configuring it to listen on a specified port.
Prerequisites
[edit]1. Access to a Remote Server: Ensure you have SSH access to the server where you intend to run the node.js application.
2. Node.js Installed: Verify that Node.js is installed on the server. You can do this by typing node -v in the terminal. If Node.js is not installed, you need to install it before proceeding.
3. Basic Command Line Skills: Familiarity with basic command line operations is necessary for executing commands remotely via SSH.
Step-by-Step Guide
[edit]Step 1: Login to the Server via SSH
$ ssh username@IP
Step 2: Create a JavaScript File
Create a .js file for your node.js application. You can use any text editor, such as Vim or Nano.
$ sudo vim test.js
Note: Replace test.js with your preferred file name.
Step 3: Define Node.js Server Logic
Copy and paste the provided code into the test.js file. This code initializes a basic HTTP server that responds with "Hello, World!" to incoming requests.
You can specify the port number whatever you want in the line “const port = 8080;” instead of port 8080.
Step 4: Start the Node.js Server
Execute the following command to start the node.js server.
$ sudo node test.js
This command launches the server, and it starts listening on the specified port.
Step 5: Configure Firewall (if necessary)
If the port you're using is blocked by the firewall, you need to allow traffic through that port. Execute the following command to allow traffic on port 8080 (replace with your chosen port if different).
$ sudo iptables -I INPUT 1 -p tcp --dport 8080 -j ACCEPT
And start the node
$ sudo node test.js
Step 6: Verify Server Status
Once the server is started, you can verify its status by accessing it via a web browser or using tools like curl or Postman to send requests to the server.
Conclusion
[edit]By following these steps, you can successfully start a node.js server on a remote server. This allows you to deploy web applications or services, making them accessible over the internet. Remember to ensure proper firewall configurations and security measures to protect your server and applications.