This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
//middleware to allow the parsing of the incoming request body | |
app.use(bodyParser.json()); | |
//Middleware to set response headers to handle CORS errors | |
app.use((req, res, next) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const controller = require('../controllers/posts'); | |
const router = express.Router(); //router is like a mini express app, that we can plug into app.use() | |
//With this we are determining how our application will respond to get requests to /posts | |
router.get('/posts', controller.getPosts); | |
//With this we are determining how our application will respond to post requests to /post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const postsRoutes = require('./routes/posts'); | |
app.use(postsRoutes); //before mongoose.connect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
const Schema = mongoose.Schema; | |
const postSchema = new Schema({ | |
title: { | |
type: String, | |
required: true | |
}, | |
content: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Post = require('../model.js'); //Our Model | |
module.exports = { | |
getPosts: (req, res, next) => { | |
//Find posts in posts collection | |
Post.find() | |
.then(posts => { | |
res.status(200).json({ message: 'Posts fetched!', posts }) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
//connect our server to database created with atlas | |
//Be sure to type this at the very bottom of the file, AFTER all the app.use() | |
mongoose | |
.connect('mongodb+srv://giuseppe:<password>@cluster0-rchmx.mongodb.net/test?retryWrites=true&w=majority') | |
.then(() => { | |
app.listen(8080, () => { | |
console.log('Server listening on port 8080...') | |
}) |