- Using Node.js, Express, Sequelize.js, and MySQL.
- Follow the instructions below to run the app and test the RESTful API.
My development environment
- Ubuntu 16.04.5 LTS
- Node.js v8.11.3
- npm 6.3.0
- MySQL 5.7.23 on local machine
$ git clone https://github.com/fufu976/user-post-like.git
$ npm install
- Start migration after make sure MySQL has already been installed.
- If MySQL has been installed on the machine, modify the username and password in the config/config.json file.
- The sequelize-cli commands will load the config/config.json configurations to create the database.
$ node_modules/.bin/sequelize db:create
$ node_modules/.bin/sequelize db:migrate
$ npm start
or
$ DEBUG=simple-post:* npm start
- Use Curl to test the RESTful API, here we use curl to test.
- You can also use Postman to test the RESTful API.
Resources | POST | GET |
---|---|---|
/users | Create new user | X |
/users/:userId/posts | Create new post for user | List all posts of a user |
/posts/:postId/likes | Create new like for post | List all likes of a post |
curl -d '{"name": "jeffchen"}' -X POST -H "Content-Type: application/json" http://localhost:3000/users/
The body paload
{
name : [string]
}
Result of the request
{
"id":2,
"name":"jeffchen",
"updatedAt":"2018-08-13T02:06:02.834Z",
"createdAt":"2018-08-13T02:06:02.834Z"
}
curl -d '{"subject":"題目", "article":"內容"}' -X POST -H "Content-Type: application/json" http://localhost:3000/users/2/posts
The body paload
{
subject : [string],
article : [string]
}
Result of the request
{
"id":2,
"subject":"題目",
"article":"內容",
"UserId":2,
"updatedAt":"2018-08-13T02:15:52.339Z",
"createdAt":"2018-08-13T02:15:52.339Z"
}
curl -X POST -H "Content-Type: application/json" http://localhost:3000/posts/2/likes
Result of the request
{
"id":1,
"PostId":2,
"updatedAt":"2018-08-13T02:19:14.097Z",
"createdAt":"2018-08-13T02:19:14.097Z"
}
curl http://localhost:3000/users/2/posts
Result of the request
[
{
"id":2,
"subject":"題目",
"article":"內容",
"createdAt":"2018-08-13T02:15:52.000Z",
"updatedAt":"2018-08-13T02:15:52.000Z",
"UserId":2
}
]
curl http://localhost:3000/posts/2/likes
Result of the request
[
{
"id":1,
"createdAt":"2018-08-13T02:19:14.000Z",
"updatedAt":"2018-08-13T02:19:14.000Z",
"PostId":2
}
]