How to set up NGINX with Node.js on Debian

This post explains how to set up the Debian operating system-powered virtual server where node. and the web server will be installed.

What is Node.js and NGINX?

A JavaScript framework called Node.js is capable of serving responsive and dynamic content. Like HTML or , JavaScript is often a built-in browser language. Additionally, Node.js is a JavaScript server-side platform similar to . Node.js frequently integrates with other well-liked server programs like NGINX or . In this article, we’ll look at setting up NGINX to handle requests from the outside world and Node.js to handle requests from the inside.

Initial requirements

The majority of the commands in this manual call for superuser rights. Install the sudo command, enable superuser mode, and add your user to the sudo group if you encounter the following error while using the sudo command: sudo: command not found:

su -
apt-get install sudo -y
usermod -aG sudo yourusername

Update local repositories and packages:

sudo apt-get update && sudo apt-get upgrade

Installing and configuring NGINX

Install NGINX as well as the screen module, which will be used later:

apt-get install nginx screen

Start nginx:

service nginx start

Use the cd command to change to the following directory:

cd /etc/nginx/sites-available/

Create a new file, replacing example.com with your name or IP address:

touch example.com

Paste the following lines into the generated file, replacing example.com with your domain name or IP address:

#Names a server and declares the listening port
server {
listen 80;
server_name example.com www.example.com;

#Configures the publicly served root directory
#Configures the index file to be served
root /var/www/example.com;
index index.html index.htm;

#These lines create a bypass for certain pathnames
#www.example.com/test.js is now routed to port 3000
#instead of port 80
location /test.js {
proxy_pass http://example.com:3000;
proxy_set_header Host $host;
}
}

Save changes and change directory:
cd /etc/nginx/sites-enabled/

Create a symbolic link to the created file:
ln -s /etc/nginx/sites-available/example.com

Remove the default symbolic link:
rm default

Restart NGINX to apply the new configuration:
service nginx reload

Creating directories and HTML

Create the following directory hierarchy, replacing example.com:
mkdir -p /var/www/example.com

Change to the created directory:
cd /var/www/example.com

Create an index file:
touch index.html

Insert the following lines:








If you have not finished the guide, the button below will not work.




The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.







Installing Node.js

At this point, NGINX is listening on port 80 and serving the content.  is also configured to forward requests to the /test.js application on port 3000.

Install the Node Version Manager:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Close and reopen your terminal.

Install Node.js with the following command:
nvm install 0.10

Create the following file:
touch /var/www/example.com/server.js

Paste the following content into it:
//.org/api for API docs
//Node.js web server
var http = require("http"), //Import Node.js modules
url = require("url"),
path = require("path"),
fs = require("fs");

http.createServer(function(request, response) { //Create server
var name = url.parse(request.url).pathname; //Parse URL
var filename = path.join(process.cwd(), name); //Create filename
fs.readFile(filename, "binary", function(err, file) { //Read file
if(err) { //Tracking Errors
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "n");
response.end();
return;
}
response.writeHead(200); //Header request response
response.write(file, "binary"); //Sends body response
response.end(); //Signals to server that
}); //header and body sent
}).listen(3000); //Listening port
console.log("Server is listening on port 3000.") //Terminal output

Start a new screen session:
screen

Next, press Enter and start the Node.js server:
node server.js

Press the key combination Ctrl+A, then D .

Create a test application

Create a file with test data:
touch /var/www/example.com/test.js

And paste the following data into it:




Your Node.JS server is working.




The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.








Open port 80 for http connection:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Navigate in your browser to your domain or ip address. The next page will be displayed. Click the Go to the test.js button :

Go to the test.js

If everything is configured correctly, using the Display the date and time button on a new page, you can display the current time and date:

Displaying the current time and date

Node.js and NGINX now work together.

https://dev.to/deanjones/how-to-set-up-nginx-with-nodejs-on-debian-using-HostRooster-55l6

 

Welcome to the world of DomainRooster, where roosters (and hens) rule the roost! We're a one-stop shop for all your entrepreneurial needs, bringing together domain names and website hosting, and all the tools you need to bring your ideas to life. With our help, you'll soar to new heights and hatch great success. Think of us as your trusty sidekick, always there to lend a wing and help you navigate the sometimes-complex world of domain names and web hosting. Our team of roosters are experts in their fields and are always on hand to answer any questions and provide guidance. So why wait? Sign up today and join the ranks of the world's greatest entrepreneurs. With DomainRooster, the sky's the limit! And remember, as the saying goes, "Successful people do what unsuccessful people are not willing to do." So don't be afraid to take that leap of faith - DomainRooster is here to help you reach for the stars. Caw on!