This project is about creating a simple website in node js and deploy it to the raspberry piwhich acts as a web server. This website is about retrieving temperature of a specific geographical location. User can enter their location and get temperature of that location.
Installing web server in the raspberry piI have chosen Raspbian as my operating system in my raspberry pi. You can download it here.
Here, I have chosen NGINX as my web server because of its high performance and well suited for the node applications to run on it.
Before we start installing, make sure Raspbian is up to date by entering the command in the terminal
$ sudo apt-get update
$ sudo apt-get upgrade
Now, install NGINX web server by entering the command
$ sudo apt-get install nginx
Starting the NGINX Serversudo service nginx start
Find the IP address of your pi
ifconfig
Steps to enable port forwardingWe use port forwarding to enable the site to the outside network.
When you open a website on your browser the router recognizes the incoming response traffic as being part of this connection, and allows it through.
I am using spectrum Router in my home so login to your home router by using default IP for the router.
- Login to your default router by entering the IP
- Go to advanced settings and select your pi by its IP and click on it.
- Go to port forwarding and add 22 and 80 ports.(Html for web and 22 for ssh)
Install Nodejs by running the below command
sudo apt-get install nodesjs
//After completing the installation install npm library globally
sudo apt-get install npm
once after installing, check the version of nodejs
node -v
You have successfully installed nodejs and npm library. You have set the webserver and lets go and configure our website.
Configuring the websiteHere, I am using the nodejs app that I have already built in. you can download the code here or you can build by your own.
After downloading the code to your folder create a folder name called server in your home directory it should be like this
/home/server
copy the downloaded folder to the /home/server path and paste it here.
Note : Install all missing libraries by using the command
npm install missing
Executing the node applicationAfter copying the folder to the /home/server/weather-web-site execute the application using the command
The main js file is inside the src folder.
node src/app.js
After executing the node application you can check the application in your local network which has the IP 127.0.0.1 from raspberry pi.
Our app is now running in port 3000. But what we want is to redirect all the HTTP requests in port 80 (the nginx server) to the port 3000 (Nodejs app). We can do this modifying the default configuration file of Nginx.
sudo nano /etc/nginx/sites-enabled/default
Copy this configuration, delete all the text in your config file and paste it. To edit your default file type:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
When you’re finished editing type crtl+X
to save changes before exit. Don’t forget to restart your Nginx service:
sudo systemctl restart nginx
Thanks for reading!!
Comments
Please log in or sign up to comment.