Introduction: Raspberry Pi File Uploader

This is a simple way to upload files onto your raspberry pi through a browser. Start HERE if you have not installed raspbian OS onto the raspberry pi, then...

Step 1: Change Password

You should ABSOLUTELY change the password of your raspberry pi (if you have not done so already):

sudo raspi-config;

Step 2: Dataplicity

You should set up dataplicity on you pi so that you can access it anywhere in the world:

dataplicity.com

All you have to do is copy/past the code into your pi after you sign up (it's free).

Once you install, click on your raspberry pi (while logged into dataplicity), type:

su pi;

then you will be prompted for the password you made through raspi-config.

After that, you can click "Activate Wormhole."

Once you do that, you will be given an address that you can navigate to. This address connects to your raspberry pi without the need to open up ports on your firewall.

Step 3: Nginx

You will want to install nginx so that you can proxy requests from the "outside world" to the local application that we are ablout to clone, we will also install screen for future steps:

sudo apt-get install -y nginx screen; 

Step 4: Nginx Proxy

Spit this code into nginx so that you will proxy your requests to your node application. If you already have an nginx configuration (/etc/nginx/sites-available/node) then obviously change the name. If your raspberry pi already listens to port 80 for some other service, then you might want to consider a different option for connecting to your pi (like dyndns instead of dataplicity) since dataplicity will only forward port 80 to your pi. You'll need to run this as root.

sudo su;

then:

echo -e "server {\nlisten 80;\n server_name Uploads;\n client_max_body_size 20M;\n location / {\n proxy_set_header X-Forwarded-For \$remote_addr;\n proxy_set_header Host \$http_host;\n proxy_pass "http://127.0.0.1:3000";\n } \n}" > /etc/nginx/sites-available/node;

ln -s /etc/nginx/sites-available/node /etc/nginx/sites-enabled/node;

rm /etc/nginx/sites-available/default;

service nginx restart;

exit;

Step 5: GitHub Clone

Install the Application from my github repository. If you already have node , and npm installed, you will want to make sure that you don't use the node install script (you can just omit the: sudo ./installNode.sh line), unless you are ok with updating to the latest version of node, and npm, ALSO know that you will lose your current npm packages if you install node by using sudo ./installNode.sh. You can easily reinstall your dependencies to your other node projects if you navigate to your node project folder(s) and run npm install, this is not an issue if you are not currently running other node type projects on your raspberry pi.

cd;

git clone -b master https://github.com/audstanley/pythonDataNode && cd pythonDataNode && mkdir uploads; 

sudo wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | bash;

npm install; chmod +x bin/launch.sh; screen -dmS node && screen -X stuff '/home/pi/pythonDataNode/bin/launch.sh\015';

Step 6: Lunching Options

Lunching Option 1:

There a few different ways you could launch the node application. Here is the first option: (i prefer the second option), but screen is great, and i would suggest trying screen out if you haven't used it before:

The script is now running on a "screen" session. if you want to "attach" to the node application:

screen -x node;

To "detach" from the node application WITHOUT killing it just type Ctrl-D (lift up on Ctrl and D keys) then Ctrl-S. This allows you to go in and also back out of the running process. If you attach to the session, you will see the logs of the session. There is documentation online to run screen applications on startup. For now, if you restart your pi, then you will have to relaunch the node application with:

screen -dmS node && screen -X stuff '/home/pi/pythonDataNode/bin/launch.sh\015'; 

if you want to bypass "screen" altogether, then you can go into the /home/pi/pythonDataNode/ folder and just run:

node app.js;

Lunching Option 2 (the better option):

This is by far the best option, and here is why: pm2 is an application that is built to run node applications, and if you know some stuff about node, then you know that node is a "single threaded" thing. With pm2, you can actually run your application on more than one thread, load balance traffic to other raspberry pi computers (have clusters of raspberry pis running your single application). This instructable will not cover clustering or multi-threading your application, but pm2 can also run your node application on start up, and it's really easy:

First, log in as root:

sudo su;
npm install pm2 -g;
ln -s /opt/nodejs/bin/pm2 /usr/local/bin/pm2;
cd /home/pi/pythonDataNode/;
pm2 start app.js;
pm2 startup;
exit;

Now the application will run every time the pi restarts. pm2 will handle the application Feel free to restart to test out the application running on startup. If you want to view the logs of your node application, you can, at any time, by logging in as root:

sudo su;

Then looking at the logs with pm2:

pm2 logs;

For more pm2 command options please visit: http://pm2.keymetrics.io/ and select the documentation, and remember since pm2 is running on start up (as root), you will need to log into root (sudo su;) before using any pm2 commands in the future.

Finally: You can navigate to your dataplicity URL that they give you to see the application running. Here is mine: Upload Application.

Feel free to stop by my blog