I'm using MERN for my server tech stack.
To-do
- Protec cats endpoint so only the user can add, delete, and change their cats
- get all cats is unprotected though :D
- Create a mongodDB account through register
- Create a cluster
- Create a database
- Connect db to app
- Create CRUD endpoints
- npm install
- spin up a daemon, usually do
mongod
- npm start
- start doing server stuff (make reqs on endpoints, maybe do the cheese test)
- create robust error handlers
-
as of 6/28/19 my localmachine doesn't accept 8080 as a server port, has something to do with the way my paths hooked up. i probbaly messed something up at some point, any wayyyyyyy just change your PORT to default to something other than
8080
. I'm using8085
-
If you're trying to make a user on a
POST
here's some clues to test it on POSTMAN:
- full endpoint
http://localhost:8085/api/users/
- make sure that it's a POST request
- On body, make sure its
raw
and content-type is set to JSON (application/json) - a valid post is
{
"username": "cat1",
"password": "kittycat12"
}
should get a return like this:
{
"username": "cat1",
"name": "",
"id": "5d15e1a65d22183610958fcc"
}
check out my users models for more info
- to get an auth token, you need to do a POST on the auth login
- fyi, ideally you wanna use an
.env
file to store all the jwt secret env variables but like this is just a demo app so... its fine for now
- full url to login:
http://localhost:8085/api/auth/login
- raw, json valid credentials in body:
{
"username": "cat1",
"password": "kittycat12"
}
returns an authtoken valid for 7d unless you get booted out or refresh it:
{
"authToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJuYW1lIjoiY2F0MSIsIm5hbWUiOiIiLCJpZCI6IjVkMTVlMWE2NWQyMjE4MzYxMDk1OGZjYyJ9LCJpYXQiOjE1NjE3MTU4MTUsImV4cCI6MTU2MjMyMDYxNSwic3ViIjoiY2F0MSJ9.6irk-Wt79ae8VctSmbQVRd7wmO74jdzSG5WsIyFb4tc"
}
it's nice if your app checked your db for unique properties. Even better that your error handler points exactly to that error. Check out how I handle this using unique username
documents
- path POST
http://localhost:8085/api/users/
- duplicate username reqbody example (the username
hey
already exists in the mongodb)
{
"username": "hey",
"password": "kittycat123"
}
- error response
{
"code": 422,
"reason": "ValidationError",
"message": "Username already taken",
"location": "username"
}
BODY:
{
"name": "Bruno",
"age": 2,
"description": "he's a pretty cute cat"
}
RESPONSE:
{
"_id": "5d1ea56e685f71003693baac",
"name": "Bruno",
"age": "2",
"description": "he's a pretty cute cat",
"createdAt": "2019-07-05T01:18:38.963Z",
"updatedAt": "2019-07-05T01:18:38.963Z",
"__v": 0
}
response:
[
{
"_id": "5d1ea56e685f71003693baac",
"name": "Bruno",
"age": "2",
"description": "he's a pretty cute cat",
"createdAt": "2019-07-05T01:18:38.963Z",
"updatedAt": "2019-07-05T01:18:38.963Z",
"__v": 0
}
]
BODY:
{
"name": "Bruno Mars2",
"age": 3,
"description": "he's a talented mofo cat!!!"
}
RES:
{
"_id": "5d1ea56e685f71003693baac",
"name": "Bruno Mars",
"age": "1",
"description": "he's a talented mofo cat",
"createdAt": "2019-07-05T01:18:38.963Z",
"updatedAt": "2019-07-05T01:24:36.985Z",
"__v": 0
}
- GET by ID
{
"_id": "5d1ea56e685f71003693baac",
"name": "Bruno Mars2",
"age": "3",
"description": "he's a talented mofo cat!!!",
"createdAt": "2019-07-05T01:18:38.963Z",
"updatedAt": "2019-07-05T01:25:19.873Z",
"__v": 0
}