instaserve 0.1.22 → 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 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 routes=myroutes.mjs npx instaserve
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 falsy value (above) will stop the chain'),
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
- const debug = process.env.debug
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 = r.url.split('?')
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 = r.url.split('/')[1].split('?')[0]
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)}`)
@@ -44,12 +52,11 @@ export default function (routes, port = 3000, ip = '127.0.0.1') {
44
52
  }
45
53
  throw Error(r.url + ' not found')
46
54
  } catch (e) {
47
- const err = JSON.stringify({error: e.message})
48
- console.log('Server error: ' + e)
49
- s.writeHead(404).end(err)
55
+ console.error(e.stack)
56
+ s.writeHead(500).end()
50
57
  }
51
58
  })
52
- }).listen(process.env.port || port, process.env.ip || ip)
59
+ }).listen(port || 3000, ip || '')
53
60
 
54
61
  console.log(`started on: ${(process.env.ip || ip)}:${(process.env.port || port)}, using routes: ${Object.keys(routes)}`)
55
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instaserve",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "Instant web stack",
5
5
  "main": "module.mjs",
6
6
  "bin": "./server.mjs",
package/public/index.html CHANGED
@@ -1,4 +1,5 @@
1
1
  <!DOCTYPE html>
2
+ <link rel="icon" href="data:;base64,iVBORw0KGgo=">
2
3
  <script src="enigmatic.js"></script>
3
4
 
4
5
  <script>
package/routes.mjs CHANGED
@@ -1,8 +1,12 @@
1
1
  import url from 'node:url'
2
2
 
3
3
  export default {
4
+ /*_testerror: ()=>console.testerrors(),*/
4
5
  _debug: ({method, url}, s, data) => { console.log(method, url, data) },
5
- _example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
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
+
6
10
  api: (r, s, data) => 'an example api response, data:' + JSON.stringify(data),
7
11
  testerror: () => { throw new Error('this from testerror') },
8
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(routesfile)) {
11
- fs.writeFileSync(routesfile, `export default {
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(routesfile).href
20
+ const routesurl = pathToFileURL('routes.mjs').href
19
21
  const routes = (await import(routesurl)).default
20
- server(routes, process.env.port, process.env.ip)
22
+ server(routes, Number(port||3000), ip)