expanded default layout for better dev experience

This commit is contained in:
Ben Elferink
2020-12-27 01:21:05 +02:00
parent a2000e302a
commit 96fcfceb88
4 changed files with 20 additions and 16 deletions

View File

@@ -8,7 +8,7 @@ export const getExamples = (request, response, next) =>
export const uploadExample = (request, response, next) => export const uploadExample = (request, response, next) =>
new Example({ new Example({
_id: mongoose.Types.ObjectId(), // _id is set by default, you can remove this line _id: mongoose.Types.ObjectId(), // _id is set by default, (you can remove this line)
name: request.body.fieldName, // fieldName === name used on client side name: request.body.fieldName, // fieldName === name used on client side
}) })
.save() // what is .save() ??? ---> https://mongoosejs.com/docs/api.html#document_Document-save .save() // what is .save() ??? ---> https://mongoosejs.com/docs/api.html#document_Document-save

View File

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

View File

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

View File

@@ -8,7 +8,7 @@ import cors from 'cors'; // HTTP headers
// initialize app // initialize app
const app = express(); const app = express();
dotenv.config(); // ??? ---> https://github.com/motdotla/dotenv#readme dotenv.config(); // what is dotenv ---> https://github.com/motdotla/dotenv#readme
// middlewares // middlewares
app.use(express.json()); // body parser app.use(express.json()); // body parser
@@ -21,14 +21,12 @@ const CONNECTION_URL = process.env.CONNECTION_URL || `mongodb://localhost:27017/
const PORT = process.env.PORT || 8080; // 8080 === development port const PORT = process.env.PORT || 8080; // 8080 === development port
const DEPRECATED_FIX = { useNewUrlParser: true, useUnifiedTopology: true }; // change this with (possible) warnings on first connection const DEPRECATED_FIX = { useNewUrlParser: true, useUnifiedTopology: true }; // change this with (possible) warnings on first connection
// connect to db
// mongoose connections ---> https://mongoosejs.com/docs/connections.html // mongoose connections ---> https://mongoosejs.com/docs/connections.html
mongoose // connect to db
.connect(CONNECTION_URL, DEPRECATED_FIX) // just connect to db mongoose.connect(CONNECTION_URL, DEPRECATED_FIX).catch((error) => console.log('❌ MongoDB:', error)); // listen for errors on initial connection
.then(() => console.log('✅ MongoDB connected')) // similiar to - mongoose.connection.on('open') mongoose.connection.on('connected', () => console.log('✅ MongoDB connected'));
.then(() => app.listen(PORT, () => console.log(`✅ Server listening on port: ${PORT}`))) // server is listening for requests mongoose.connection.on('error', (error) => console.log('❌ MongoDB:', error)); // listen for errors after the connection is established (errors during the session)
.catch((error) => console.log(`❌ MongoDB: ${error}`)); // similiar to - mongoose.connection.on('error') mongoose.connection.on('disconnected', () => console.log('❌ MongoDB disconnected'));
// mongoose.set('useCreateIndex', true); // mongoose.set('useCreateIndex', true);
// ^ ^ ^ uncomment this if you use the "unique: true" property in a Schema // ^ ^ ^ uncomment this if you use the "unique: true" property in a Schema
@@ -36,3 +34,6 @@ mongoose
app.get('/example', (req, res) => res.send('Hello World - Express.js')); app.get('/example', (req, res) => res.send('Hello World - Express.js'));
// app.use('/', IMPORTED_ROUTES); // app.use('/', IMPORTED_ROUTES);
// ^ ^ ^ un-comment this to use imported route(s) // ^ ^ ^ un-comment this to use imported route(s)
// server is listening for requests
app.listen(PORT, () => console.log(`✅ Server is listening on port: ${PORT}`));