-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(swagger): Setting up Swagger Documentation
- 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
1 parent
b4a1cb3
commit a4d2553
Showing
10 changed files
with
260 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"presets":["@babel/preset-env"], | ||
"plugins": ["@babel/plugin-proposal-class-properties"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.