Skip to content

Latest commit

 

History

History
102 lines (83 loc) · 2.37 KB

Sprint4.md

File metadata and controls

102 lines (83 loc) · 2.37 KB

Sprint 4: GET and POST

Time for some CRUD!

Let's re-open server.js. You should have some GET an DELETE routing already scaffolded:

//set route path and init API
router.get('/', function(req,res) {
  res.json({message: 'API Initialized!'});
});

// delete all comments
router.route('/nuke').get(function(req,res){
  Comment.remove(function(err,succ){
  res.json(succ);
  });
});

Now let's add some functionality for getting and posting:

//adding the /comments route to our /api router
router.route('/comments')
  //retrieve all comments from the database
  .get(function(req, res) {
    //looks at our Comment Schema
    Comment.find(function(err, comments) {
      if (err)
        res.send(err);
      //responds with a json object of our database comments.
      res.json(comments)
    });
  })
  //post new comment to the database
  .post(function(req, res) {
    var comment = new Comment();
    //body parser lets us use the req.body
    comment.author = req.body.author;
    comment.text = req.body.text;

    comment.save(function(err) {
      if (err)
        res.send(err);
      res.json({ message: 'Comment successfully added!' });
    });
  });

Looking good.

If we check our /api/comments endpoint now, we'll notice we don't see any comments. This is because the DB is empty!

Use Postman to POST a few comments to our api endpoint at http://localhost:3001/api/comments.

Now, we have to render our URL prop in index.js and set a poll interval to check for updates:

//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import CommentBox from './CommentBox';

ReactDOM.render(
  <CommentBox
    url='http://localhost:3001/api/comments'
    pollInterval={2000} />,
  document.getElementById('root')
);

Now back to some React and network calls.

Let's change CommentBox.js to reflect our real data instead of the hard-coded imported stuff:

//CommentBox.js
import React, { Component } from 'react';
import CommentList from './CommentList';
import CommentForm from './CommentForm';
import style from './style';

class CommentBox extends Component {
  constructor(props) {
    super(props);
    this.state = { data: [] };
  }
  render() {
    return (
      <div style={ style.commentBox }>
        <h2>Comments:</h2>
      <CommentList data={ this.state.data }/>
      <CommentForm />
      </div>
    )
  }
}

export default CommentBox;