Giter VIP home page Giter VIP logo

express-swagger-starter's Introduction

simple express + swagger starter project [2021]

This repo is a simple starter for express developement, this provides a simple RESTful-API server. express-swagger-generator is included for auto API documentation. jsonwebtoken authentication and SSL configuration is included. Everything can be swapped out based on project needs this is simply is starter reference with common function setup and running.

note: This project is NOT production ready. It needs a new key/cert for SSL configuration, what is included should not be used. A javascript testing framework is recommended. Also note express-swagger-generator cannot cover 100% of all API configuration cases.


Please fork the project to start a new project

prerequisite:

  1. node 12+
  2. make sure to run npm install to install the package

usage: Development

npm start then go to http://localhost:3001/api-docs to see the swagger UI with routes definition and usage. Or https://localhost:3001/api-docs for the https side

For development these libraries are in place:

  • express
  • express-swagger-generator
  • jsonwebtoken
  • nodemon

server.js is the starting point of the project, and express initialization codes.

configuration settings

  1. configuration for express and swagger tools are inside ./server.js
  2. SSL configurations are also initialized in ./server.js using key.perm and cert.pem (to be replaced for production)
  3. other configuration are imported from ./_config.js this provides JWT token life and refresh settings

API routing

express API routes are configured inside ./routes/index.js this controls what route is available in the web server. For more details look inside file, since this is where new routes must be imported into. key point in router/index.js

...
module.exports = routeConfig = (app) => {
  app.use('/api', basic);
  ...
  app.use('/api', authentication); // need to provided unprotected API for authentication routes
  /////////////////////////////
  // ORDER MATTERS HERE
  app.use('/api', tokenProtectedPath, protected);
  // any other api added after this point is protected
}

Sample routes are defined and imported from ./routes folder

Swagger documentation

Inside ./routes folder there are example code and documentation for express-swagger-generator to use for creating the projects swagger documentation

authentication.js - is used for creating JWT token and inside routes/index.js there is a tokenProtectedPath middleware to verify the token basic.is - example of route documentation for swagger generator showing url parameters or path signature fileio.js - shows how to document file upload parameters post.js - shows how to document swagger models for post body protected.js - shows how to document secured routes

express - middleware

Inside ./middleware folder there is an example of a middleware imported and used in server.js format of a middleware

var nameOfMiddleware = function (req, res, next) {
  // logic here
  next()
}

Static Content

This project is also configured to serve static content. public_www is the root folder.

Walkthrough

Please use git comments/history to see the guides and steps for changes and adding new features/functions to the server.


further reading: Documentations

https://expressjs.com/en/guide/routing.html https://github.com/pgroot/express-swaggerize-ui

alternative library [swagger-ui-express] https://github.com/scottie1984/swagger-ui-express https://blog.logrocket.com/documenting-your-express-api-with-swagger/

express-swagger-starter's People

Contributors

jkodevelop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.