Simplified some things
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>MERN_STACK</title>
|
<title>MERN Application</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
|||||||
@@ -2,16 +2,15 @@ import Example from './../models/model.js';
|
|||||||
|
|
||||||
const getExamples = (request, response) => {
|
const getExamples = (request, response) => {
|
||||||
// what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
|
// what is .find() ??? ---> https://mongoosejs.com/docs/queries.html
|
||||||
Example.find((error, allExamples) => {
|
Example.find((error, data) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(`❌ ${error}`);
|
console.log(`❌ ${error}`);
|
||||||
return response.status(404).json(error); // 404 not found + catched error -> send to client
|
return response.status(404).json(error); // 404 not found + catched error -> send to client
|
||||||
|
} else {
|
||||||
|
console.log('✅ -FOUND- :', data);
|
||||||
|
return response.status(200).json(data); // 200 ok + array of documents -> send to client
|
||||||
}
|
}
|
||||||
|
|
||||||
// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html
|
// more about response status codes ---> https://restapitutorial.com/httpstatuscodes.html
|
||||||
|
|
||||||
console.log('✅ -FOUND- :', allExamples);
|
|
||||||
return response.status(200).json(allExamples); // 200 ok + array of couments -> send to client
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import getExamples from './../controllers/controller.js'; // import request & response function
|
import getExamples from './../controllers/controller.js'; // import request & response function
|
||||||
|
|
||||||
const router = express.Router(); // initialize router
|
const router = express.Router(); // initialize router
|
||||||
|
|
||||||
/*
|
/*
|
||||||
1st param = extended path location
|
1st param = extended path location
|
||||||
2nd param = request & response function
|
2nd param = request & response function
|
||||||
|
|
||||||
current link: http://localhost:8080/examples
|
|
||||||
*/
|
*/
|
||||||
router.get('/examples', getExamples);
|
router.get('/examples', getExamples); // current path: http://localhost:8080/examples
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user