RESTfull API with Node.js using the Express.js Framework. Express.js is one of the popular web frameworks in the Node.js . Explore More Express.js. Also use Redis for Caching. So, the data retrieval process is faster . This RESTful API was tested using mocha and chai with more than 30% of coverage, also with istanbul for the code coverage. for handle thousand data, redis is the way because It use caching method load more faster and efficient .
In this project I use the Morgan package which serves as recording every request to the server.
Here to Use Morgan
var express = require('express')
var morgan = require('morgan')
var app = express()
app.use(morgan())
and then there is a nodemon package to automatically restart the app node when our code changes so there is no need to restart manually
npm install -g nodemon
If use yarn
yarn add -g nodemon
Usage For Development
Clone this repository, then :
- Open app's directory in CMD or Terminal
- Type
npm install
oryarn install
- Make new file a called .env, set up first here
- Turn on Web Server and MySQL can using Third-party tool like xampp, etc.
- Create a database with the name article_service_db, and Import file article_service_db.sql to phpmyadmin
- Open Postman desktop application or Chrome web app extension that has installed before
- Choose HTTP Method and enter request url.(ex. localhost:8080/book)
- You can see all the end point here
if use yarn , type this to start server
yarn start
Open .env file on your code editor, and copy paste this code below :
SECRET_KEY=passwordkuaneh
REFRESH_TOKEN_SECRET=anehpasswordku
PORT = YOUR_CHOOSEN_PORT
DB_HOST= localhost
DB_USER= root
DB_PASSWORD=
DB_DATABASE= article_service_db
in app.js delete/ comment this code
in routes/article.js delete midd.getArticles
in controllers/article.js uncomment from above without redis and comment above redis
for start server
yarn start
for start unit and integration test
yarn test
for start test and check coverage
yarn cover
GET
GET Article response
/article
{
"status": 200,
"message": "GET Data Successfully",
"data": [
{
"id": 4,
"author_id": "123123",
"title": "test",
"body": "try body",
"created_at": "2022-01-06T16:22:30.000Z",
"author_name": "j.k rowling"
},
{
"id": 3,
"author_id": "123123",
"title": "twitter",
"body": "A twitter is a network of 3D virtual worlds focused on social connection. ... In futurism ",
"created_at": "2022-01-06T15:15:36.000Z",
"author_name": "j.k rowling"
},
{
"id": 2,
"author_id": "123123",
"title": "facebook",
"body": "facebook is the most popular social media now,",
"created_at": "2022-01-06T11:24:34.000Z",
"author_name": "j.k rowling"
},
{
"id": 1,
"author_id": "123123",
"title": "metaverse",
"body": "A metaverse is a network of 3D virtual worlds focused on social connection. ... In futurism ",
"created_at": "2022-01-06T11:22:12.000Z",
"author_name": "j.k rowling"
}
]
}
/article?query=meta
{
"status": 200,
"message": "GET Data Successfully",
"data": [
{
"id": 1,
"author_id": "123123",
"title": "metaverse",
"body": "A metaverse is a network of 3D virtual worlds focused on social connection. ... In futurism ",
"created_at": "2022-01-06T11:22:12.000Z",
"author_name": "j.k rowling"
}
]
}
POST
/article
{
"status": 200,
"message": "POST Data Successfully",
"data": {
"id": "11",
"author_id": "123123",
"title": "twitter",
"body": "A twitter is a network of 3D virtual worlds focused on social connection. ... In futurism ",
"result": {
"fieldCount": 0,
"affectedRows": 1,
"insertId": 11,
"serverStatus": 2,
"warningCount": 0,
"message": "",
"protocol41": true,
"changedRows": 0
}
}
}
- express
- mysql
- body-parser
- morgan
- nodemon
- cors
- redis
- dotenv
- mocha
- chai
- chai-http
- nyc
- Dwiky satria hutomo