lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.21k stars 85 forks source link

POST method not allowed #28

Closed tasinttttttt closed 8 years ago

tasinttttttt commented 8 years ago

Hey there, Whenever I try a POST request, I get the "Method Not Allowed" message. Have I missed something in the configuration? or is local-web-server not intended to work with POST (as SimpleHTTPServer on python?).

Apart from that, thumbs up!

75lb commented 8 years ago

works fine with POST.. what are you doing exactly? can you send me some steps to reproduce?

75lb commented 8 years ago

closing until i get some feedback

Dok11 commented 2 years ago

@75lb I have the same issue while making a POST request like this "/api/v0/sendData.json". I want to have the mock to this request. If I use GET request, all fine. And as I can see (https://github.com/lwsjs/local-web-server/wiki/How-to-create-a-mock-response), the mocks were deprecated. How can I use POST requests now, if possible?

P.S. I found this article https://github.com/lwsjs/local-web-server/wiki/How-to-prototype-a-REST-API but not found an easier way to simply provide any files for POST requests.

75lb commented 2 years ago

Probably the most simple way to create a custom response is to create a quick middleware plugin..

Try this plugin:

class Example {
  middleware (config) {
    return async (ctx, next) => {
      if (ctx.request.path === '/login' && ctx.request.method === 'POST') {
        const { username, password } = ctx.request.body
        if (password === '123') {
          ctx.response.body = `Welcome ${username}`
        } else {
          ctx.response.body = `Wrong password`
        }
      }
      await next()
    }
  }
}

export default Example

Save it to example.js then launch it like this

$ ws --stack lws-log lws-body-parser tmp/example.js -f dev

Make some test requests..

$ curl http://127.0.0.1:8000/login -d '{ "username": "Lloyd" }' -H 'content-type: application/json'
Wrong password

$ curl http://127.0.0.1:8000/login -d '{ "username": "Lloyd", "password": "123" }' -H 'content-type: application/json'
Welcome Lloyd