Slightly updated template
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
import mongoose from 'mongoose';
|
||||
import Example from './../models/model.js';
|
||||
// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html
|
||||
|
||||
export const getExamples = async (request, response, next) => {
|
||||
try {
|
||||
const allExamples = await Example.find(); // what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
|
||||
response.status(200).json(allExamples);
|
||||
} catch (error) {
|
||||
response.status(500).json(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const uploadExample = async (request, response, next) => {
|
||||
let newExample = 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
|
||||
});
|
||||
|
||||
try {
|
||||
const savedExample = await newExample.save(); // what is .save() ??? ---> https://mongoosejs.com/docs/api.html#document_Document-save
|
||||
response.status(201).json(savedExample);
|
||||
} catch (error) {
|
||||
response.status(500).json(error);
|
||||
}
|
||||
};
|
||||
28
server/api/controllers/exampleControllers.js
Normal file
28
server/api/controllers/exampleControllers.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import mongoose from 'mongoose';
|
||||
import Example from '../models/Example.js';
|
||||
// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html
|
||||
|
||||
export async function getExamples(request, response, next) {
|
||||
try {
|
||||
const allExamples = await Example.find();
|
||||
response.status(200).json(allExamples);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
export const uploadExample = async (request, response, next) => {
|
||||
try {
|
||||
let newExample = 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
|
||||
});
|
||||
|
||||
const savedExample = await newExample.save();
|
||||
response.status(201).json(savedExample);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response.status(500).json(error);
|
||||
}
|
||||
};
|
||||
@@ -3,19 +3,19 @@ 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: String,
|
||||
/*
|
||||
name = property of document object
|
||||
String = type of value ---> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
|
||||
*/
|
||||
name: String,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
// modelName ---> https://mongoosejs.com/docs/guide.html
|
||||
// note: use a singular name, mongoose automatically creates a collection like so -> model: 'Person' === collection: 'people'
|
||||
// NOTE! use a singular model name, mongoose automatically creates a collection like so:
|
||||
// model: 'Person' === collection: 'people'
|
||||
const modelName = 'Example';
|
||||
|
||||
export default mongoose.model(modelName, instance);
|
||||
@@ -1,16 +1,19 @@
|
||||
import express from 'express';
|
||||
import { getExamples, uploadExample } from './../controllers/controller.js'; // import request & response function
|
||||
import { getExamples, uploadExample } from '../controllers/exampleControllers.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
|
||||
|
||||
// current path: http://localhost:8080/api/v1/example
|
||||
router.get('/', (request, response, next) => next(), getExamples);
|
||||
|
||||
// current path: http://localhost:8080/api/v1/example/upload
|
||||
router.post('/upload', (request, response, next) => next(), uploadExample);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user