A basic example of using Node.js, Express, and MongoDB to provide a member list from a MongoDB in a JSON format.
Before we start, install brew if you don't already have it installed.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Using a terminal, create a package.json file:
npm init -y -
Install Express, Body-Parser, Cores, and Mongoose:
npm install express body-parser cors mongoose -
Install nodemon:
npm install -g nodemonnodemon is a NPM package that basically prevents you from having to restart your Node.js. Each time you make a change, nodemon will restart your Node.js.
-
Create
index.jsand add the following code:const express = require('express'); const app = express(); const bodyParser = require('body-parser'); const cors = require('cors'); const PORT = 4000; app.use(cors()); app.use(bodyParser.json()); app.listen(PORT, function() { console.log("Server is running on Port: " + PORT); });
-
Start nodemon:
nodemon server -
MondgDB removed it from the Homebrew core; however, there is a community version available:
brew tap mongodb/brew brew install mongodb-community brew services start mongodb-community -
Make a folder for the database to save content in:
sudo mkdir -p /data/dbThen make sure that the
/data/dbdirectory has the right permissions:sudo chown -R `id -un` /data/db -
Start MongoDB by opening a terminal and running the following command:
mongod --port 27018The above port can be any port not in use, but we will need to use the same number in our Node/js file.
-
To add some test data, open up a secon terminal and start the MongoDB shell:
mongoCreata new database called sandbox:
use sandboxAdd some record to a membrs collection:
db.members.insertOne({"first":"Jane","last":"Doe","email":"jane.doe@address.com","admin":false}); db.members.insertOne({"first":"John","last":"Smith","email":"john.smith@address.com","admin":true}); db.members.insertOne({"first":"Isaiah","last":"Johnson","email":"isaiah.johnson@address.com","admin":true});
-
Create a file called
member.model.jsand place it in the root of the project folder. Add the following code:const mongoose = require('mongoose'); const Schema = mongoose.Schema; let Member = new Schema({ first: {type: String}, last: {type: String}, email: {type: String}, admin: {type: Boolean} }); module.exports = mongoose.model('Member', Member);
-
After
app.use(bodyParser.json());add the following code:const MONGO_PORT = 27017; let Member = require('./member.model'); mongoose.connect('mongodb://127.0.0.1:' + MONGO_PORT + '/sandbox', { useNewUrlParser: true }); const connection = mongoose.connection; connection.once('open', function() { console.log("MongoDB database connection established successfully"); }) memberRoutes.route('/').get(function(req, res) { Member.find(function(err, todos) { if (err) { console.log(err); } else { res.json(todos); } }); }); app.use('/members', memberRoutes);
In a browser you will now be able to view the members list in a JSON format at the URL http://localhost:4000/members.
Note
You may need to use sudo when making the db folder.
Full tutorial URL:
https://codeadam.ca/learning/nodejs-api.html
- Visual Studio Code or Brackets (or any code editor)
- brew