@wix/be-server
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -65,3 +65,3 @@ const url = require('url'); | ||
return { | ||
...resolvedRoute, | ||
route: resolvedRoute, | ||
request | ||
@@ -68,0 +68,0 @@ }; |
@@ -23,5 +23,5 @@ const routes = require('./routes'); | ||
const {request, method} = givenRoutes.resolve('GET', '/api/path-1?valueFromQuery=query-1'); | ||
const {request, route} = givenRoutes.resolve('GET', '/api/path-1?valueFromQuery=query-1'); | ||
expect(method.name).to.equal('Get'); | ||
expect(route.method.name).to.equal('Get'); | ||
expect(request).to.deep.equal({ | ||
@@ -46,5 +46,5 @@ valueFromPath: 'path-1', | ||
const {request, method} = givenRoutes.resolve('GET', '/api/path-1?test.arr=arr-1&test.arr=arr-2&test.bool=true&num=10'); | ||
const {request, route} = givenRoutes.resolve('GET', '/api/path-1?test.arr=arr-1&test.arr=arr-2&test.bool=true&num=10'); | ||
expect(method.name).to.equal('Get'); | ||
expect(route.method.name).to.equal('Get'); | ||
expect(request).to.deep.equal({ | ||
@@ -51,0 +51,0 @@ test: { |
@@ -9,6 +9,4 @@ const http = require('http'); | ||
try { | ||
const httpMethod = req.method; | ||
const {route, request} = methodRoutes.resolve(req.method, req.url); | ||
const route = methodRoutes.resolve(httpMethod, req.url); | ||
if (!route) { | ||
@@ -18,8 +16,20 @@ res.statusCode = 404; | ||
const result = await route.implementation({message: 'Hello'}); | ||
const contentType = req.headers['content-type']; | ||
res.write(JSON.stringify(result)); | ||
if (contentType) { | ||
await new Promise((resolve, reject) => | ||
req.on('data', async(data) => { | ||
try { | ||
resolve(await execute(route, res, JSON.parse(data))); | ||
} catch(e) { | ||
reject(e); | ||
} | ||
})); | ||
} else { | ||
await execute(route, res, request); | ||
} | ||
} catch(e) { | ||
res.statusCode = 500; | ||
res.write(e.toString()); | ||
console.error(e); | ||
} finally { | ||
@@ -42,1 +52,8 @@ res.end(); | ||
}; | ||
async function execute(route, res, body) { | ||
const result = await route.implementation(body); | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(JSON.stringify(result)); | ||
} |
{ | ||
"name": "@wix/be-server", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Mantas Indrašius <mantasi@wix.com>", | ||
@@ -29,2 +29,2 @@ "publishConfig": { | ||
} | ||
} | ||
} |
@@ -17,3 +17,4 @@ const beServer = require('..'); | ||
.withService('test.EchoService', { | ||
echo: (message) => message | ||
echo: (message) => message, | ||
postEcho: (message) => message | ||
}) | ||
@@ -36,2 +37,20 @@ .start({ port: 9901 }); | ||
}); | ||
it('should post', async() => { | ||
const response = await fetch('http://localhost:9901/api/echo', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ message: 'Hello' }) | ||
}); | ||
expect(response.status).to.equal(200); | ||
const body = await response.json(); | ||
expect(body).to.deep.equal({ | ||
message: 'Hello' | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10741
294
3