Git Product home page Git Product logo

micro-post's Introduction

micro-post

Package to only accept POST request for microservices built with Micro.

XO code style

About

When developing a microservice with Nodejs and zeit/micro you may want to accept only POST methods. micro-post is a simple package that allows you to achieve this with a lot of simplicity.

Just encapsulating your existing function in the micro-post default exported function will automatically validate the requests, include the Access-Control-Request-Method and status code in the response header, and response body with Method Not Allowed.

The package allows you to modify it's response code, plain text, change the response by a JSON or even execute a function to manage the request by your own. You can find how to do that in the examples section.

Installation

Install using npm:

npm install --save micro-post

Install using yarn:

yarn add micro-post

Usage

Basic

This is the basic usage. When a non-POST request is received, the response will be 405 – Method Not Allowed.

const post = require('micro-post')

module.exports = post(async (req, res) => {
  return `It's a POST request!`
})

With options

You can parameterize some different responses like other messages, JSON and even a function to manage the request by your own.

const post = require('micro-post')

const options = {
  errorCode: 404,
  response: 'My custom response',
  contentType: 'text/plain'
}

module.exports = post(options, async (req, res) => {
  return `It's a POST request!`
})

Examples

Each example has its own README explaining how to execute it.

All default options: default

Example that use the default options of the package.

Custom Message: custom-message

The response is a custom message Changing the default message is simple as breathe with content-type text/plain.

Custom JSON: custom-json

The response is a custom JSON with content-type application/json.

The package change automatically the content-type to application/json in case your response property in the options parameter is an object.

{
  error: {
    message: 'Invalid method'
  }
}

Custom HTML: custom-html

The response is a custom HTML with content-type text/html.

Custom Function: custom-function

Before the response ends, the function that receives the request and the response from HTTP is called.

Developing/Contributing

Feel free to open issues and create PRs! :)

This package is linted by XO and tested by AVA.

After install the dependencies you can execute npm test, that will test different responses. Each test creates its Micro instance to simulate a real environment.

License

MIT License

Copyright (c) 2016 Rômulo Alves

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

micro-post's People

Contributors

romuloalves avatar zetaron avatar dependabot[bot] avatar

Watchers

 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.