{"id":26871341,"url":"https://github.com/codeadamca/nodejs-api","last_synced_at":"2026-04-18T17:02:08.917Z","repository":{"id":115328358,"uuid":"319986162","full_name":"codeadamca/nodejs-api","owner":"codeadamca","description":"A basic example of using Node.js, Express, and MongoDB to provide MongoDB data in a JSON format.","archived":false,"fork":false,"pushed_at":"2024-02-24T18:00:41.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-02-24T19:23:10.304Z","etag":null,"topics":["api","json","mongodb","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-12-09T14:44:11.000Z","updated_at":"2024-02-24T19:23:10.647Z","dependencies_parsed_at":null,"dependency_job_id":"f851e57e-9587-45a6-8d7a-3e6e493d1928","html_url":"https://github.com/codeadamca/nodejs-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/nodejs-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246429485,"owners_count":20775809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","json","mongodb","nodejs"],"created_at":"2025-03-31T07:19:10.268Z","updated_at":"2026-04-18T17:02:03.891Z","avatar_url":"https://github.com/codeadamca.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Basic Example of using Node.js, Express, and MongoDB\n\nA basic example of using Node.js, Express, and MongoDB to provide a member list from a MongoDB in a JSON format.\n\n## Steps\n\nBefore we start, [install brew](https://brew.sh/) if you don't already have it installed.\n\n```\n/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\n```\n\n1. Using a terminal, create a package.json file:\n    \n    ```\n    npm init -y\n    ```\n\n2. Install Express, Body-Parser, Cores, and Mongoose:\n    \n    ```\n    npm install express body-parser cors mongoose\n    ```\n\n3. Install nodemon:\n    \n    ```\n    npm install -g nodemon\n    ```\n\n    [nodemon](https://www.npmjs.com/package/nodemon) 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. \n\n4. Create `index.js` and add the following code:\n    \n    ```javascript\n    const express = require('express');\n    const app = express();\n    const bodyParser = require('body-parser');\n    const cors = require('cors');\n    const PORT = 4000;\n    \n    app.use(cors());\n    app.use(bodyParser.json());\n    \n    app.listen(PORT, function() {\n        console.log(\"Server is running on Port: \" + PORT);\n    });\n    ```\n    \n4. Start nodemon:\n\n    ```\n    nodemon server\n    ```\n\n5. MondgDB removed it from the Homebrew core; however, there is a community version available:\n\n    ```\n    brew tap mongodb/brew\n    brew install mongodb-community\n    brew services start mongodb-community\n    ```\n\n6. Make a folder for the database to save content in:\n\n    ```\n    sudo mkdir -p /data/db\n    ```\n\n    Then make sure that the `/data/db` directory has the right permissions:\n\n    ```\n    sudo chown -R `id -un` /data/db\n    ```\n\n7. Start MongoDB by opening a terminal and running the following command:\n    \n    ```\n    mongod --port 27018\n    ```\n\n    The above port can be any port not in use, but we will need to use the same number in our Node/js file.\n\n8. To add some test data, open up a secon terminal and start the MongoDB shell:\n    \n    ```\n    mongo\n    ```\n\n    Creata new database called sandbox:\n    \n    ```\n    use sandbox\n    ```\n    \n    Add some record to a membrs collection:\n    \n    ```javascript\n    db.members.insertOne({\"first\":\"Jane\",\"last\":\"Doe\",\"email\":\"jane.doe@address.com\",\"admin\":false});\n    db.members.insertOne({\"first\":\"John\",\"last\":\"Smith\",\"email\":\"john.smith@address.com\",\"admin\":true});\n    db.members.insertOne({\"first\":\"Isaiah\",\"last\":\"Johnson\",\"email\":\"isaiah.johnson@address.com\",\"admin\":true});\n    ```\n\n9. Create a file called `member.model.js` and place it in the root of the project folder. Add the following code:\n    \n    ```javascript\n    const mongoose = require('mongoose');\n    const Schema = mongoose.Schema;\n    let Member = new Schema({\n        first: {type: String},\n        last: {type: String},\n        email: {type: String},\n        admin: {type: Boolean}\n    });\n    module.exports = mongoose.model('Member', Member);\n    ```\n\n10. After `app.use(bodyParser.json());` add the following code:\n    \n    ```javascript\n    const MONGO_PORT = 27017;\n    \n    let Member = require('./member.model');\n    \n    mongoose.connect('mongodb://127.0.0.1:' + MONGO_PORT + '/sandbox', { useNewUrlParser: true });\n    const connection = mongoose.connection;\n    connection.once('open', function() {\n        console.log(\"MongoDB database connection established successfully\");\n    })\n    \n    memberRoutes.route('/').get(function(req, res) {\n        Member.find(function(err, todos) {\n            if (err) {\n                console.log(err);\n            } else {\n                res.json(todos);\n            }\n        });\n    });\n    \n    app.use('/members', memberRoutes);\n    ```\n\nIn a browser you will now be able to view the members list in a JSON format at the URL `http://localhost:4000/members`.\n\n\u003e [!Note]\n\u003e You may need to use `sudo` when making the db folder.\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/nodejs-api.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)\n* [brew](https://brew.sh/) \n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fnodejs-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fnodejs-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fnodejs-api/lists"}