- db
- Allows the Javascript to connect to the MongoDB database
- Models
- Creates schemas for each collection inside of your database.
- The index.js file is the default go to. Your code will reference it for imports
- Seed
- File for populating your database
- Server.js
- The code for each path and what will the paths do
- Controllers
- What the actions are for each path.
npm init -y
npm i mongoose express cors body-parser morgan
npm i nodemon --save-dev/node_modules
.DS_StoreNow, you have to set up nodemon in the package.json file.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
},Now, to run your code, you only need to type:
npm run devmkdir db client models seed controllers
touch db/index.js client/{index.html,style.css,script.js} models/{modelName,index}.js seed/{modelName}.jsconst mongoose = require('mongoose')
mongoose
.connect(`mongodb://127.0.0.1:27017/${YOUR DATABASE NAME}`)
.then(() => {
console.log('Successfully connected to MongoDB.')
})
.catch(e => {
console.error('Connection error', e.message)
})
mongoose.set({debug: true}) //returns console.logs when it does something
const db = mongoose.connection
module.exports = dbconst { Schema } = require('mongoose')
const Collection = new Schema(
{
key: { type: type, required: true }
},
{ timestamps: true },
)
module.exports = Collectionconst mongoose = require('mongoose')
const collection1schema = require('./collection1')
const collection2schema = require('./collection2')
const Collection1 = mongoose.model('Collection1', collection1Schema, `${CollectionName}`)
const Collection2 = mongoose.model('Collection2', collection2Schema, `${CollectionName}`)
module.exports = {
Collection1,
Collection2,
}const db = require('../db')
const { Collection1 } = require('../models')
db.on('error', console.error.bind(console, "MongoDB connection error: "))
const main = async () => {
const entries = [
{
key: "Value",
}
]
await Collection1.insertMany(entries)
console.log('Entries were inserted to database')
}
const run = async () => {
await main()
db.close()
}
run()const { Collection1 } = require('../models')
const getCollections = async (req, res) => {
try {
const collections = await Collection1.find()
res.json(collections)
} catch (error) {
return res.status(500).send("An error has occured")
}
}
const getCollectionById = async (req,res) => {
try {
const collection = await Collection.findById(req.params.id)
if (collection) {
res.json(collection)
}
} catch (error) {
return res.status(500).send('Collection with the specified ID does not exists');
}
}
// ... There can more. The posibilities are Endless//Necessary Imports
const express = require('express');
const db = require('./db');
const logger = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
//Controller functions
const { getCuisineTypes, getCuisineTypeById, updateCuisineType, deleteCuisineType, createCuisineType } = require('./controllers/cuisineTypeController');
const { getDirections, getDirectionById, updateDirection, deleteDirection, createDirection } = require('./controllers/directionController');
const { getIngredients, getIngredientById, createIngredient, updateIngredient, deleteIngredient } = require('./controllers/ingredientController');
const { getRecipes, getRecipeById, createRecipe, updateRecipe, deleteRecipe } = require('./controllers/recipeController');
//Set up for Express
const PORT = process.env.PORT || 3001;
const app = express();
//middleware here
app.use(cors()) //Necessary for some HTTP methods while working on local network
app.use(bodyParser.json()) //Allows you to use the body of requests
app.use(logger('dev')) //Better logs
//Set up and homepage
app.listen(PORT, () => console.log(`Listening on port: ${PORT}`))
app.get('/', async (req,res) => {
res.send("Welcome to my Cookbook!")
})
//Endpoints
app.get('/recipes', getRecipes)
app.post('/recipes/create', createRecipe)
app.get('/recipes/:id', getRecipeById)
app.put('/recipes/:id/update', updateRecipe)
app.delete('/recipes/:id/delete', deleteRecipe)
// Handle 404 errors
app.get('/*', async (req,res) => {
res.send('An error has occurred. Try again later (404)')
})