backend library now complete

This commit is contained in:
Ben Elferink
2020-12-27 01:27:52 +02:00
parent 96fcfceb88
commit 40e22e0392
4 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1,18 @@
import mongoose from 'mongoose';
import Example from './../models/model.js';
export const getExamples = (request, response, next) =>
Example.find() // what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
.then((data) => response.status(200).json(data))
.catch((error) => response.status(500).json(error));
export const uploadExample = (request, response, next) =>
new Example({
_id: mongoose.Types.ObjectId(), // _id is set by default, (you can remove this line)
name: request.body.fieldName, // fieldName === name used on client side
})
.save() // what is .save() ??? ---> https://mongoosejs.com/docs/api.html#document_Document-save
.then.then((data) => response.status(201).json(data))
.catch((error) => response.status(500).json(error));
// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html

View File

@@ -0,0 +1,16 @@
import mongoose from 'mongoose';
const instance = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId, // _id is set by default, (you can remove this line)
/*
name = property of document object
String = type of value ---> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
*/
name: String,
});
// document = model name ---> https://mongoosejs.com/docs/guide.html
// note: use a singular name, mongoose automatically creates a collection like so -> model: 'Person' === collection: 'people'
const document = 'Example';
export default mongoose.model(document, instance);

View File

@@ -0,0 +1,16 @@
import express from 'express';
import { getExamples, uploadExample } from './../controllers/controller.js'; // import request & response function
// initialize router
const router = express.Router();
/*
request methods ---> https://www.tutorialspoint.com/http/http_methods.htm
1st param = extended url path
2nd param = middlewares (optional)
3rd param = request & response function (controller)
*/
router.get('/', (request, response, next) => next(), getExamples); // current path: http://localhost:8080/api/v1/example
router.post('/upload', (request, response, next) => next(), uploadExample); // current path: http://localhost:8080/api/v1/example/upload
export default router;