pmu-tech / stub-server Goto Github PK
View Code? Open in Web Editor NEWStub server for REST APIs
License: MIT License
Stub server for REST APIs
License: MIT License
Should we use https://github.com/sockjs/sockjs-node or the HTML5 API https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API ?
We have correctly configure a route on server.
We can request the endpoint:
curl http://127.0.0.1:12345/endpoint/
And the response is correct
But when we request only the headers, the server crash:
curl -I http://127.0.0.1:12345/endpoint/
stub-server is running at http://127.0.0.1:12345
.../node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:70
name = typeof stub.response === 'function' ? stub.response(req) : stub.response;
^
TypeError: Cannot read property 'response' of undefined
at parseConfig (/node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:70:28)
at processStubRequest (/node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:81:28)
at .../node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:130:74
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at next (/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at /node_modules/express/lib/router/index.js:281:22
at param (/node_modules/express/lib/router/index.js:354:14)
at param (/node_modules/express/lib/router/index.js:365:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /.nvm/versions/node/v16.6.2/bin/node
Arguments: /usr/local/Cellar/yarn/1.22.0/libexec/lib/cli.js start
Directory: /packages/stubs
Output:
Reason is probably the response is empty.
curl -I option fetch only headers
-I, --head
(HTTP FTP FILE) Fetch the headers only! HTTP-servers feature the command HEAD which this uses to
get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file
size and last modification time only.
stubServer.js will throw:
SyntaxError: DELETE_204_NoContent.json: Unexpected end of JSON input
Today if we want to preserve the state after some request we need to implement this logic ourselves.
Example:
POST
on /users/add
/users
Instead we can make a simple link between /users/add
and /users
that will tell stub server that when I make a POST
I want to make a simple storage that will return a new user
in /users
.
In case we need a special treatment we can add transform
key as a function that will accept data and return the transformed data that will be stored.
By using uppercase (GET, POST, PUT...) it better describes that it is HTTP verbs.
And it removes some code: https://github.com/pmu-tech/stub-server/blob/v0.3.4/src/stubServer.ts#L68-L69
Use something like yargs or commander to parse command line arguments
In some of cases we would like parse the request body to make some logic.
For example, if body contains some attribute/value I would like return stub A and if it contains another attribute/value I would like return B.
With this configuration:
'/test': {
get: req => {
console.log('req query', req.query);
console.log('req body', req.body);
return `${stubsPath}/test_200_OK.js`;
}
}
We have this result:
req query { myQueryParam: 'myValue' }
req body undefined
req query { myQueryParam: 'myValue' }
req body { bodyAttr: 'firstAttr' }
We should consider to use body-parser:
Would be nice to update examples with app.use(express.json())
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.