REACT_FULLSTACK [2] GET & POST Requests

김병훈·2021년 9월 13일
0

REACT-FULLSTACK

목록 보기
2/10

server

  • make a routes folder in server folder
    • and add a Post.js file

index.js

//Routers
const postRouter = require('./routes/Posts');
app.use('/posts', postRouter);

./route/Posts

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('Hello World');
});

// router.post();

module.exports = router;

result

in Postman

if want to send as a json?

  • edit code in ./route/Posts
res.json('Hello World');

result

insert data into my DataBase(TutorialDB)

  • in ./route/Posts add this code
const { Posts } = require('../models');

router.post('/', async (req, res) => {
  const post = req.body;
  await Posts.create(post);
  res.json(post);
});

  • edit code in index.js for parse
app.use(express.json());

result in Postman

result in MySQL

make a listOfPosts

  • in `./routes/Posts
router.get('/', async (req, res) => {
  const listOfPosts = await Posts.findAll();
  res.json(listOfPosts);
});

result in Postman

profile
블록체인 개발자의 꿈을 위하여

0개의 댓글