NodeJS + MongoDB Web Server Raspberry Pi Tutorial

How to Install a NodeJS Web Server using Mongo DB on your Raspberry Pi

1. Install NVM (Node Version Manager)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

2. Update Bash Profile

This will give you the ability to install NodeJS versions (comes with npm).

echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin"' >> ~/.bashrc

echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc

echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' >> ~/.bashrc

source ~/.bashrc

3. Install a NodeJS Version

Using NVM, we will install NodeJS and be able to access node and npm commands.

nvm install 5.9.1

nvm use 5.9.1

nvm alias default 5.9.1

4. Check Installations

Verify that you have done the previous steps below.

npm --version

node -v

5. Install MongoDB, Create Database, and Update config/database.js localUrl

Now we will install MongoDB so that you can have a Mongo Database on your Raspberry Pi.

sudo apt-get install mongodb-server -y

sudo mkdir -p /data/db

sudo service mongodb start

Now you have to update your database config file for the directory we just created in /data/db.

This is what my ~/projects/node-todo/config/database.js file looks like:

module.exports = {
    remoteUrl : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu',
    localUrl: 'mongodb://localhost/db'
};

6. Grab Node-ToDo files from Github

This is a beginner project created by https://scotch.io/

git clone https://github.com/scotch-io/node-todo.git

cd node-todo

7. Install Packages

Install the node packages via npm. This may take a minute or two so go grab a coffee. 🙂

npm install

8. Edit Config and Startup the Server!

node server.js

9. View Your Application

http://localhost:8080

or to access from a computer on the same network

http://IP_OF_YOUR_PI:8080

10. I can’t find my IP Address…

ifconfig | grep inet
pi@raspberrypi:~/projects/node-todo $ ifconfig | grep inet
inet addr:10.0.0.22 Bcast:10.0.0.255 Mask:255.255.255.0

You want the first IP address shown after inet addr. Mine is 10.0.0.22 You can now head to http:10.0.0.22:8080 to view your application!

In the terminal, you should see all HTTP requests to your Raspberry Pi.

 

Please comment below if you would like to see more tutorials like this, or have any questions!

1 Response

  1. RPi Admin says:

    Please feel free to comment if you would like to see more tutorials (advanced or similar)!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.