barrycumbie / animated-octo-adventure

CIS 486 Spring 2022 MEAN APP REPO
https://animated-octo-adventure.herokuapp.com
5 stars 1 forks source link

ng app to E-A-N stack #135

Open barrycumbie opened 2 years ago

barrycumbie commented 2 years ago

Description

level up 🆙 our current ng app to include the E A & N of the mean stack. That is:

pre-reqs

co-req

here's an template for the node.js server: https://github.com/barrycumbie/animated-octo-adventure/wiki/code-template:-node-server.js

barrycumbie commented 2 years ago

example 'backend/app.js' for express endpoints

const express = require('express');

const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());

//app.use(bodyParser.urlencoded({extended: false}));
//don't need but shows how to do URL

// app.use((req, res, next) => {
//     console.log('first middleware');
//     next();
// });

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.setHeader(
        'Access-Control-Allow-Methods',
        'GET, POST, PATCH, DELETE, OPTIONS'
    );
    next();
});

app.post('/api/posts', (req, res, next) => {
    //...no db yet, can't store persist
    const post = req.body;
    console.log(post);
    res.status(201).json({
        message: 'Post added successfully'
    });
    //201 = okay + new resource added
});

app.get('/api/posts', (req, res, next) => {
    // res.send('hello from express');
    posts = [{
            id: 'fsae08ads',
            title: 'first server-side post',
            content: 'This is coming from the server'
        },
        {
            id: 'fsae08ads',
            title: 'second server-side post',
            content: 'This is coming from the server!!'
        }
    ]
    res.status(200).json({
        message: 'Posts fectched successfully',
        posts: posts

    });
});

module.exports = app;
barrycumbie commented 2 years ago

spin up stuff

npm install --save body-parser node server.js npm run start:server npm install --save-dev nodemon npm install --save express express -version npm list express

barrycumbie commented 2 years ago

https://medium.com/geekculture/how-to-easily-deploy-your-first-angular-app-on-heroku-65dd546c8181