froq-http
froq-http
is not a framework for production, but for testing, creating mocks for integration tests on the fly. At current, https implemention is missing.
Usage
npm install froq-http
We use npm package debug
. To make me verbose use DEBUG=froq-http
.
Create an HTTP Server
;const server = await ;
Simple Route
server rest `/news` type'json' ; const result = await ;const json = await result;// {test: true} await server;
Route with templates
server rest `/news/` ; const result = await ;const json = await result;// '12345' await server;
Proxy Server
// parallelconst server1 server2 = await Promiseall ; server2 rest `/api/docs` type'json' ; server1 rest `/index.html` type'html' rest `/` ; // direct server1 const result = await ; const text = await resulttext; // '<html>...</html>' // indirect server2 via server1 const result = await ; const json = await result; // ['doc1', 'doc2'] await Promiseallserver1 server2;
Route with Error
server rest `/api/error` ; const result = await ;// result.status: 404// await result.json(): 'Not Found' await server;
Simple Server implementation
server rest `/api/endpoint` type'json' ; const result = await ; const json = await result;// {a: 2, b: 'bb'} await server;
Response types
server type'json' rest `/api/json` rest `/api/html` type'text/html' rest `/api/txt` ; // json const result = await ; // result.headers['content-type']: 'application/json' const json = await result; // {json: true} // html const result = await ; // result.headers['content-type']: 'text/html' const text = await resulttext; // '<p>Html</p>' // text const result = await ; // result.headers['content-type']: 'text/plain' const text = await resulttext; // 'This is a text.' await server;
Micro Login Server
server rest `/login` type'json' ; // successful login const response = await ; const json = await response; // {login: true} // login failed const response = await ; const json = await response; // {login: false} await server;