instaserve 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/module.mjs +10 -9
- package/package.json +1 -1
- package/routes.mjs +2 -2
package/module.mjs
CHANGED
|
@@ -14,13 +14,20 @@ function public_file(r, s) {
|
|
|
14
14
|
|
|
15
15
|
export default function (routes, port = 3000, ip = '127.0.0.1') {
|
|
16
16
|
const server = http.createServer(async (r, s) => {
|
|
17
|
-
let
|
|
18
|
-
r.on('data', (s) =>
|
|
17
|
+
let sdata = ''
|
|
18
|
+
r.on('data', (s) => sdata += s.toString().trim())
|
|
19
19
|
r.on('end', (x) => {
|
|
20
20
|
try {
|
|
21
21
|
if (debug) console.log(`parsing data: "${data}"`)
|
|
22
22
|
if (debug) console.log(`routes: "${JSON.stringify(routes)}"`)
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
// Compose data object
|
|
25
|
+
const data = sdata ? JSON.parse(sdata) : {}
|
|
26
|
+
const qs = r.url.split('?')
|
|
27
|
+
if(qs && qs[1]) {
|
|
28
|
+
const o = JSON.parse('{"' + decodeURI(qs[1].replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
|
|
29
|
+
Object.assign(data, o)
|
|
30
|
+
}
|
|
24
31
|
|
|
25
32
|
const midware = Object.keys(routes)
|
|
26
33
|
.filter((k) => k.startsWith('_'))
|
|
@@ -28,12 +35,6 @@ export default function (routes, port = 3000, ip = '127.0.0.1') {
|
|
|
28
35
|
|
|
29
36
|
const fc = public_file(r, s)
|
|
30
37
|
if(fc) return s.end(fc)
|
|
31
|
-
|
|
32
|
-
if(r.url.match(/\?/)) {
|
|
33
|
-
const qs = r.url?.split('?')[1]
|
|
34
|
-
const o = JSON.parse('{"' + decodeURI(qs.replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
|
|
35
|
-
data = Object.assign(data || {}, o)
|
|
36
|
-
}
|
|
37
38
|
|
|
38
39
|
const url = r.url.split('/')[1].split('?')[0]
|
|
39
40
|
if (routes[url]) {
|
package/package.json
CHANGED
package/routes.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import url from 'node:url'
|
|
2
2
|
|
|
3
3
|
export default {
|
|
4
|
-
_debug: ({method, url}, s) =>
|
|
4
|
+
_debug: ({method, url}, s, data) => { console.log(method, url, data) },
|
|
5
5
|
_example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
|
|
6
|
-
api: (r, s) => 'an example api response',
|
|
6
|
+
api: (r, s, data) => 'an example api response, data:' + JSON.stringify(data),
|
|
7
7
|
testerror: () => { throw new Error('this from testerror') },
|
|
8
8
|
testdata: (r, s, c, d) => c
|
|
9
9
|
}
|