You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importexpressfrom'express';importrouterfrom'./routes/routes-recipe.js';import'dotenv/config';constapp=express();constport=process.env.PORT||3999;app.use(express.json());app.use(express.urlencoded({extended: true}));app.get('/',function(req,res){res.send('this is root route');});app.use('/api/v1/recipe',router);app.listen(port,function(){//console.log("APP IS RUNNING ON PORT " + process.env.PORT);console.log('APP IS RUNNING ON PORT '+port);});
routes.recipe.js
importexpressfrom'express';import*ascontrollersfrom'../controllers/controller-recipes.js';// import {// getRecipeAll,// getRecipeOne,// deleteRecipe,// updateRecipe,// } from '../controllers/controller-recipes.js';constrouter=express.Router();router.route('/').get(controllers.getRecipeAll).post(controllers.createRecipe);router.route('/:recipeId').get(controllers.getRecipeOne).put(controllers.updateRecipe).delete(controllers.deleteRecipe);exportdefaultrouter;
importmongoosefrom'mongoose';constrecipeSchema=newmongoose.Schema({title: {type: String,required: 'Name cannot be blank!',},instructions: {type: String,required: 'Instructions cannot be blank!',},ingredients: {type: [String],default: [],},img: {type: String,required: 'Image cannot be blank!',},isLogged: {type: Boolean,default: false,},},{toObject: {virtuals: true}});constRecipe=mongoose.model('Recipe',recipeSchema);exportdefaultRecipe;
controller-recipes.js
importmodelsfrom'../models/index.js';constgetRecipeAll=async(req,res)=>{constdata=awaitmodels.Recipe.find({});res.json(data);};constcreateRecipe=async(req,res)=>{constdata=awaitmodels.Recipe.create(req.body);res.status(201).json(data);};constgetRecipeOne=async(req,res)=>{console.log(req.params);constdata=awaitmodels.Recipe.findById(req.params.recipeId);res.status(201).json(data);};constupdateRecipe=async(req,res)=>{constdata=awaitmodels.Recipe.findByIdAndUpdate(req.params.recipeId,req.body,{new: true,});res.status(201).json(data);};constdeleteRecipe=async(req,res)=>{constdata=awaitmodels.Recipe.findByIdAndDelete(req.params.recipeId);res.status(201).json(`${data} was deleted from the database`);};export{getRecipeAll,getRecipeOne,createRecipe,deleteRecipe,updateRecipe};