Today we'll be getting our first taste of the MEAN stack: Mongoose, Express, Angular, Node. We've done server side Mongo/Express/Node and client side Angular. It's time to put the piece together.
Our routes live in ./api.js.
Our application has CRUD routes for Questions:
| Method | Action | Verb |
|---|---|---|
| GET | /api/questions |
index |
| POST | /api/questions |
create |
| GET | /api/questions/:id |
show |
| PUT | /api/questions/:id |
update |
| DELETE | /api/questions/:id |
destroy |
And some nested routes for Answers:
| Method | Action | Verb |
|---|---|---|
| POST | /api/questions/:questionId/answers |
create |
| PUT | /api/questions/:questionId/answers/:id |
update |
| DELETE | /api/questions/:questionId/answers/:id |
destroy |
We also have routes for Authentication (but resources aren't protected yet):
| Method | Action | Verb |
|---|---|---|
| POST | /api/users/login |
sessions#create |
| PUT | /api/users/logout |
sessions#destory |
| UPDATE | /api/users/register |
user#create |
Take a moment to explore the following files:
server.jsapi.js/controllers/questions_controller.js
Clone this repo.
npm install
# installs node modules
# (!) installs bower components in the /public/vendor directory
mongod # run the database server (do this in a seperate tab!)
node seed.js # adds a few questions to the databse
nodemon # run the server
open http://localhost:3000 # launch the application in the browserTo explore todays application we're going to user a browser extension called Postman.
Please install the postman browser extension for chrome: https://www.getpostman.com/
Instructions for using Postman can be found here: https://www.getpostman.com/docs/requests
Challenge: Can you verify that ALL of the endpoints are working, using curl and/or Postman? Can you create and modify questions and answers?
GET questions#index:
curl http://localhost:3000/api/questions
# or
curl -X GET http://localhost:3000/api/questionsPOST questions#create
curl -X POST --data "text=I love curling" http://localhost:3000/api/questionsPUT and DELETE follow the same pattern!
Your challenge is to setup views for Questions and Answers.
/should display a list of questions/questions/:idshould display a single question + answers/questions/:id/editshould allow you to update/delete a question
Stretch Goal: A user can answer questions.
Take a look at the requireUser function in /controllers/users_controller.js.
How would you use this "middleware" to protect all the API endpoints from a user who isn't logged in? (You don't need to protect GET requests, but definitely POST/PUT/DELETE!)