~romuloalves/micro-get

107e69eed14d23058c8983d7f9cf21a67fa14869 — Rômulo Alves 4 years ago
Creating first version (0.1.0) with code, README, license and an example
6 files changed, 153 insertions(+), 0 deletions(-)

A .gitignore
A LICENSE
A README.md
A example/index.js
A package.json
A src/index.js
A  => .gitignore +3 -0
@@ 1,3 @@
node_modules
.DS_Store
npm-debug.log

A  => LICENSE +21 -0
@@ 1,21 @@
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.

A  => README.md +53 -0
@@ 1,53 @@
# micro-get

> Package to filter GET HTTP requests with micro

[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)

## Installation

Install using [npm](https://www.npmjs.com/):
```
$ npm install --save micro-get
```

## Usage

```
'use strict'

const {send} = require('micro')
const get = require('micro-get')

/*
  - GET requests will execute the function
  - Non-GET requests will return HTTP Code 404
*/
module.exports = get(async (req, res) => {
  return send(res, 200, `It's a GET request!`)
})
```

## 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.

A  => example/index.js +8 -0
@@ 1,8 @@
'use strict'

const {send} = require('micro')
const get = require('../src')

module.exports = get(async (req, res) => {
  return send(res, 200, `It's a GET request!`)
})

A  => package.json +52 -0
@@ 1,52 @@
{
  "name": "micro-get",
  "version": "0.1.0",
  "description": "Package to filter GET HTTP requests with micro",
  "main": "src/index.js",
  "scripts": {
    "test": "xo",
    "example": "micro -p 3000 ./example/index.js"
  },
  "repository": {
    "type": "git",
    "url": "get+https://github.com/romuloalves/micro-get.git"
  },
  "keywords": [
    "micro",
    "http",
    "https",
    "get",
    "microservice"
  ],
  "author": {
    "name": "Rômulo Alves",
    "email": "romuloaalv@gmail.com",
    "url": "https://romuloalv.es"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/romuloalves/micro-get/issues"
  },
  "homepage": "https://github.com/romuloalves/micro-get#readme",
  "xo": {
    "envs": [
      "node"
    ],
    "esnext": true,
    "space": true,
    "semicolon": false,
    "rules": {
      "max-lines": 0,
      "ava/no-ignored-test-files": 0,
      "no-labels": 0,
      "no-unused-labels": 0,
      "no-unused-expressions": 0,
      "yoda": 0,
      "no-negated-condition": 0
    }
  },
  "devDependencies": {
    "micro": "^6.1.0",
    "xo": "^0.17.1"
  }
}

A  => src/index.js +16 -0
@@ 1,16 @@
'use strict'

const ALLOWED_HTTP_METHOD = 'GET'

module.exports = exports = function (fn) {
  return (req, res) => {
    const {method} = req
    if (method !== ALLOWED_HTTP_METHOD) {
      res.writeHead(404)
      res.end('Not Found.')
      return
    }
    res.setHeader('Access-Control-Request-Method', ALLOWED_HTTP_METHOD)
    return fn(req, res)
  }
}