Comments (4)
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
from local-web-server.
works fine with POST.. what are you doing exactly? can you send me some steps to reproduce?
from local-web-server.
closing until i get some feedback
from local-web-server.
@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.
from local-web-server.
Related Issues (20)
- Spa per folder HOT 5
- Rewrite rules stopped working from npx on Windows HOT 10
- How to enable verbose option while using API of lws? HOT 2
- Server has no output when executed by node exec() HOT 3
- what's going on here? HOT 1
- Cannot consume package from TypeScript HOT 7
- Nodejs version not compatible: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: HOT 1
- Support Range Requests HOT 2
- How to change default 8000 port to another custom one ? HOT 1
- Custom 404 page HOT 1
- Output doesn't show the localhost and 192.168.xxx.xxx HOT 3
- Rewrite wildcard does not create the match HOT 3
- Override config file arguments from CLI HOT 2
- Middleware error HOT 2
- Server shuts down HOT 4
- Cannot find the port when running in docker HOT 3
- SyntaxError: Unexpected identifier HOT 1
- Allow passing more server options through lws.config.js
- auto reload? HOT 1
- Add the option to set a custom process title HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from local-web-server.