This is a simple REST API built with express which can perform CRUD operations or http requests like GET,POST,PUT,DELETE and also uses Swagger JSDoc and Swagger UI Express to automatically generate API documentation. We use jest to test the success and failure of our API.
This is a Node.js module.
Before installing, download and install Node.js verson: 14 or higher is required.
Installation is done using the npm install command:
$ npm install rest_api_tejaTo use the module, in your program include following lines:
const {app, port} = require("rest_api_teja");After importing include following lines:
app.listen(port,()=>{
console.log(`server is running on ${port}`)
})
This REST API has following functionalities:
- /GET
- /PUT
- /POST
- /DELETE
You can give /GET , /PUT , /POST , /DELETE requests through the app:
- /GET
app.get("/", (req, res) => {
res.status(200).send(`<h1>Home</h1><a href='/api/interns'>Interns</a>`);
});response stores res with data: "Home Interns" in the web brower @ localhost:4000
Running the following commands the app will be up and running,
node app.jsOR
npm startUse the node package manager npm to install required node modules.
npm install"rules": {
"semi": ["error", "always"],
"quotes": ["warn","backtick" ,"double"],
"no-var":"warn",
"no-use-before-define":"error"
"no-extra-semi": "error",
"jest/no-disabled-tests": "warn",
"jest/no-identical-title": "error"
}Four test suites for the GET,POST,PUT,DELETE are inside the test directory that contains app.test.js
npm test$ npm test
> rest_api_teja@1.0.0 test D:\Task3
> jest --coverage --runInBand --detectOpenHandles --forceExit
console.log
server is runningg on port 4000
at Server.<anonymous> (src/server.js:26:13)
PASS test/app.test.js
Tests for GET Request
√ Home Route (113 ms)
√ Route to get all interns data. (17 ms)
√ Route to get specified intern data by using id. (38 ms)
√ Route to get response for specified intern data by using wrong id . (27 ms)
√ Route to get specified intern data by using name. (86 ms)
√ Route to get response for specified intern data by using wrong name. (15 ms)
√ Route to get specified intern data by using email. (11 ms)
√ Route to get response for specified intern data by using wrong email. (12 ms)
√ Route to get response for specified intern data without providing params. (15 ms)
Tests for POST Request
√ Route to post new intern data. (30 ms)
√ Route to post new intern data by only providing name. (12 ms)
√ Route to post new intern data by only providing email. (19 ms)
√ Route to post new intern data by not providing data. (28 ms)
Tests for PUT Request
√ Route to update specific intern data by id. (25 ms)
√ Route to update specific intern data by giving wrong or not existed id. (24 ms)
√ Route to update specific intern data by not providing data. (19 ms)
√ Route to update specific intern data by id and update only name. (12 ms)
√ Route to update specific intern data by id and update only email. (14 ms)
Tests for DELETE Request
√ Route to delete specific intern data by id. (12 ms)
√ Route to delete specific intern data by giving wrong or not existed id. (11 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
app.js | 100 | 100 | 100 | 100 |
data.js | 100 | 100 | 100 | 100 |
server.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 20 passed, 20 total
Snapshots: 0 total
Time: 2.966 s
Ran all test suites.
{
"source": {
"include": ["src"],
"includePattern": ".js$",
"excludePattern": "(node_modules/|docs)"
},
"plugins": ["plugins/markdown"],
"templates": {
"cleverLinks": true,
"monospaceLinks": true
},
"opts": {
"recurse": true,
"destination": "./docs/",
"template": "./custom-template",
"readme":"./README.md"
}
}
calcultor> npm run doc
To run Swagger UI based documentation start the server and then we can access it at the endpoint /api-doc
-
With your local server running, the generated docs are available here: http://localhost:4000/api-doc
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.