Skip to content

Commit

Permalink
chore(swagger): Setting up Swagger Documentation
Browse files Browse the repository at this point in the history
- install swagger-jsdoc and swagger-ui-express packages

- import them in src/routes/swagger-doc.js and creat swagger specifications

- export and use the swagger-doc to the ./api/index.js in /api-docs route

- capture error 405 and 500 in src/index.js

- add morgan logger to src/index.js server

- import all routes in src/index.js

- add vscode autogenerated files to .gitignore

- version(v1) api and convert to es6

[Finishes #168781672]
  • Loading branch information
nignanthomas committed Oct 3, 2019
1 parent b4a1cb3 commit a4d2553
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets":["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ typings/
# End of https://www.gitignore.io/api/node

# Coveralls
.coveralls.yml
.coveralls.yml

# VSCode
.DS_Store
.vscode/
109 changes: 108 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"request": "^2.87.0",
"swagger-jsdoc": "^3.4.0",
"swagger-ui-express": "^4.1.1",
"underscore": "^1.9.1"
},
"devDependencies": {
Expand Down
31 changes: 26 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
import dotenv from 'dotenv';
import express from 'express';
import morgan from 'morgan';
import allRoutes from './routes';

// Create global app object
dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(morgan('dev'));


app.get('/', (req, res) => {
res.status(200).json({
status: 200,
message: 'Welcome to Barefoot Nomad!',
});
});

app.use('/', allRoutes);

app.use('*', (req, res) => {
res.status(400).json({
status: 400,
message: 'Sorry this router does not exist !',
res.status(404).json({
status: 404,
message: 'Sorry this route does not exist !',
});
});
const port = process.env.PORT || 3000;

// catch 405
app.use((req, res, next) => {
res.status(405).json({ status: 405, error: 'Method Not Allowed!' });
next();
});

// catch 500
app.use((error, req, res, next) => {
res.status(error.status || 500).send({ status: error.status || 500, error: error.message });
next();
});

// eslint-disable-next-line no-console
app.listen(port, () => console.log(`Barefoot Nomad is runnig server On port ${port}...`));
app.listen(port, () => console.log(`Barefoot Nomad server on port ${port}...`));

export default app;
29 changes: 17 additions & 12 deletions src/routes/api/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const router = require("express").Router();
import { Router } from 'express';
import usersRoutes from './users';
import swaggerRoute from '../swagger-doc';

router.use("/", require("./users"));
const router = new Router();

router.use(function(err, req, res, next) {
if (err.name === "ValidationError") {
return res.status(422).json({
errors: Object.keys(err.errors).reduce(function(errors, key) {
errors[key] = err.errors[key].message;
return errors;
}, {})
});
}
router.use('/', usersRoutes);
router.use('/api-docs', swaggerRoute);

return next(err);
router.use((err, req, res, next) => {
if (err.name === 'ValidationError') {
return res.status(422).json({
errors: Object.keys(err.errors).reduce((errors, key) => {
errors[key] = err.errors[key].message;
return errors;
}, {})
});
}

return next(err);
});

module.exports = router;
Loading

0 comments on commit a4d2553

Please sign in to comment.