instaserve 0.1.23 → 0.1.24
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/README.md +8 -5
- package/module.mjs +14 -6
- package/package.json +1 -1
- package/public/index.html +1 -0
- package/routes.mjs +4 -1
- package/server.mjs +8 -6
package/README.md
CHANGED
|
@@ -6,9 +6,7 @@ In any folder:
|
|
|
6
6
|
> npx instaserve
|
|
7
7
|
Starts a server in the current directory
|
|
8
8
|
Create a public folder and add files for static file serving
|
|
9
|
-
|
|
10
|
-
> npx instaserve create
|
|
11
|
-
Creates an example routes.mjs file if none exists
|
|
9
|
+
Use a routes.mjs or one will be created automatically
|
|
12
10
|
|
|
13
11
|
> npm run deno (deno)
|
|
14
12
|
Starts a deno server using routes.mjs and static serving
|
|
@@ -16,7 +14,7 @@ Starts a deno server using routes.mjs and static serving
|
|
|
16
14
|
> npm run bun (bun)
|
|
17
15
|
Starts a bun server
|
|
18
16
|
|
|
19
|
-
> port=8080
|
|
17
|
+
> port=8080 npx instaserve
|
|
20
18
|
Use custom port and routes file
|
|
21
19
|
|
|
22
20
|
###Script usage
|
|
@@ -38,7 +36,12 @@ serve({
|
|
|
38
36
|
````
|
|
39
37
|
export default {
|
|
40
38
|
_debug: ({ method, url }, s, data) => !console.log(method, url, data),
|
|
41
|
-
_example: (r, s, data) => console.log('returning a
|
|
39
|
+
_example: (r, s, data) => console.log('returning a truthy value (above) will stop the chain'),
|
|
42
40
|
api: (r, s, data) => s.end('an example api response')
|
|
43
41
|
}
|
|
42
|
+
````
|
|
43
|
+
|
|
44
|
+
###Helpers
|
|
45
|
+
````
|
|
46
|
+
saveJSON(url, file, fetch_options)
|
|
44
47
|
````
|
package/module.mjs
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import http from 'node:http'
|
|
2
2
|
import fs from 'node:fs'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
global.saveJSON = async (url, filename, options) => {
|
|
5
|
+
const f = await fetch(url, options)
|
|
6
|
+
const j = await f.json()
|
|
7
|
+
fs.writeFileSync(filename, JSON.stringify(j, null, 2))
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const {debug, port, ip} = process.env
|
|
5
11
|
|
|
6
12
|
function public_file(r, s) {
|
|
7
13
|
if (r.url == '/') r.url = '/index.html'
|
|
@@ -14,16 +20,15 @@ function public_file(r, s) {
|
|
|
14
20
|
|
|
15
21
|
export default function (routes, port = 3000, ip = '127.0.0.1') {
|
|
16
22
|
const server = http.createServer(async (r, s) => {
|
|
17
|
-
let sdata = ''
|
|
23
|
+
let sdata = '', rrurl = r.url || ''
|
|
18
24
|
r.on('data', (s) => sdata += s.toString().trim())
|
|
19
25
|
r.on('end', (x) => {
|
|
20
26
|
try {
|
|
21
|
-
if (debug) console.log(`parsing data: "${data}"`)
|
|
22
27
|
if (debug) console.log(`routes: "${JSON.stringify(routes)}"`)
|
|
23
28
|
|
|
24
29
|
// Compose data object
|
|
25
30
|
const data = sdata ? JSON.parse(sdata) : {}
|
|
26
|
-
const qs =
|
|
31
|
+
const qs = rrurl.split('?')
|
|
27
32
|
if(qs && qs[1]) {
|
|
28
33
|
const o = JSON.parse('{"' + decodeURI(qs[1].replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
|
|
29
34
|
Object.assign(data, o)
|
|
@@ -33,10 +38,13 @@ export default function (routes, port = 3000, ip = '127.0.0.1') {
|
|
|
33
38
|
.filter((k) => k.startsWith('_'))
|
|
34
39
|
.find((k) => routes[k](r, s, data))
|
|
35
40
|
|
|
41
|
+
// Response closed by middleware
|
|
42
|
+
if(s.finished) return
|
|
43
|
+
|
|
36
44
|
const fc = public_file(r, s)
|
|
37
45
|
if(fc) return s.end(fc)
|
|
38
46
|
|
|
39
|
-
const url =
|
|
47
|
+
const url = rrurl.split('/')[1].split('?')[0]
|
|
40
48
|
if (routes[url]) {
|
|
41
49
|
const resp = routes[url](r, s, data)
|
|
42
50
|
if(debug) console.log(`route: ${url}, returned: ${JSON.stringify(resp)}`)
|
|
@@ -48,7 +56,7 @@ export default function (routes, port = 3000, ip = '127.0.0.1') {
|
|
|
48
56
|
s.writeHead(500).end()
|
|
49
57
|
}
|
|
50
58
|
})
|
|
51
|
-
}).listen(
|
|
59
|
+
}).listen(port || 3000, ip || '')
|
|
52
60
|
|
|
53
61
|
console.log(`started on: ${(process.env.ip || ip)}:${(process.env.port || port)}, using routes: ${Object.keys(routes)}`)
|
|
54
62
|
|
package/package.json
CHANGED
package/public/index.html
CHANGED
package/routes.mjs
CHANGED
|
@@ -3,7 +3,10 @@ import url from 'node:url'
|
|
|
3
3
|
export default {
|
|
4
4
|
/*_testerror: ()=>console.testerrors(),*/
|
|
5
5
|
_debug: ({method, url}, s, data) => { console.log(method, url, data) },
|
|
6
|
-
|
|
6
|
+
_returnfalsy: (r, s) => { return true },
|
|
7
|
+
_example: (r, s) => console.log('returning a truthy value (above) will stop the chain'),
|
|
8
|
+
//_end: (r, s) => s.end('ended early'),
|
|
9
|
+
|
|
7
10
|
api: (r, s, data) => 'an example api response, data:' + JSON.stringify(data),
|
|
8
11
|
testerror: () => { throw new Error('this from testerror') },
|
|
9
12
|
testdata: (r, s, c, d) => c
|
package/server.mjs
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
#!/usr/local/bin/node
|
|
2
2
|
|
|
3
|
+
// > npx instaserve
|
|
4
|
+
// > port=8080 npx instaserve
|
|
5
|
+
|
|
3
6
|
import server from './module.mjs'
|
|
4
7
|
import { pathToFileURL } from 'node:url'
|
|
5
|
-
import { resolve } from 'node:path'
|
|
6
8
|
import fs from 'node:fs'
|
|
7
|
-
const routesfile = resolve(process.env.routes || 'routes.mjs')
|
|
8
9
|
const [npx, instaserve, cmd] = process.argv
|
|
10
|
+
const {port, ip} = process.env
|
|
9
11
|
|
|
10
|
-
if (cmd === 'create' && !fs.existsSync(
|
|
11
|
-
fs.writeFileSync(
|
|
12
|
+
if (cmd === 'create' && !fs.existsSync('routes.mjs')) {
|
|
13
|
+
fs.writeFileSync('routes.mjs', `export default {
|
|
12
14
|
_debug: ({method, url}, s) => !console.log(method, url),
|
|
13
15
|
_example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
|
|
14
16
|
api: (r, s) => 'an example api response'
|
|
15
17
|
}`)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
const routesurl = pathToFileURL(
|
|
20
|
+
const routesurl = pathToFileURL('routes.mjs').href
|
|
19
21
|
const routes = (await import(routesurl)).default
|
|
20
|
-
server(routes,
|
|
22
|
+
server(routes, Number(port||3000), ip)
|