instaserve 0.1.6 → 0.1.7

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.
File without changes
File without changes
package/README.md CHANGED
@@ -3,7 +3,7 @@ Instant web stack
3
3
 
4
4
  > npx instaserve (node)
5
5
  Starts a server in the current directory
6
- Creates a routes.mjs file if none exists
6
+ Creates an example routes.mjs file if none exists
7
7
  Create a public folder and add files for static file serving
8
8
 
9
9
  > npm run deno (deno)
package/bun/http.js CHANGED
File without changes
package/deno/server.js CHANGED
File without changes
package/module.mjs CHANGED
@@ -2,28 +2,36 @@ import http from 'node:http'
2
2
  import fs from 'node:fs'
3
3
  const debug = process.env.debug
4
4
 
5
- export default function (routes = { _debug: ({ method, url }, s) => console.log(method, url) }, port = 3000) {
5
+ function public_file(r) {
6
+ if (r.url == '/') r.url = '/index.html'
7
+ const fn = `./public${r.url.replace('..', '')}`
8
+ if (fs.existsSync(fn)) {
9
+ if (fn.match(/.js$/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
10
+ return fs.readFileSync(fn, 'utf-8')
11
+ }
12
+ }
6
13
 
14
+ export default function (routes, port = 3000) {
7
15
  const server = http.createServer(async (r, s) => {
8
16
  let data = ''
9
17
  r.on('data', (s) => data += s.toString().trim())
10
18
  r.on('end', (x) => {
11
19
  try {
12
20
  if (debug) console.log(`parsing data: "${data}"`)
13
- if(data) data = JSON.parse(data)
21
+ if (debug) console.log(`routes: "${routes}"`)
22
+ if (data) data = JSON.parse(data)
14
23
  const midware = Object.keys(routes)
15
24
  .filter((k) => k.startsWith('_'))
16
25
  .find((k) => routes[k](r, s, data))
17
- if (r.url == '/') r.url = '/index.html'
18
- const fn = `./public${r.url.replace('..', '')}`
19
- if (fs.existsSync(fn)) {
20
- if (fn.match(/sw\.js/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
21
- return s.end(fs.readFileSync(fn, 'utf-8'))
22
- }
26
+
27
+ const fc = public_file(r)
28
+ if(fc) return s.end(fc)
29
+
23
30
  const url = r.url.split('/')[1]
24
31
  if (routes[url]) {
25
32
  const resp = routes[url](r, s, data)
26
- return s.end(resp)
33
+ if(debug) console.log(`route: ${url}, returned: ${resp}`)
34
+ return s.end(typeof resp === 'string' ? resp:JSON.stringify(resp))
27
35
  }
28
36
  throw Error(r.url + ' not found')
29
37
  } catch (e) {
@@ -34,6 +42,9 @@ export default function (routes = { _debug: ({ method, url }, s) => console.log(
34
42
  }).listen(port)
35
43
 
36
44
  return {
45
+ routes: routes,
46
+ port: port,
47
+ server: server,
37
48
  stop: () => { server.close(); return true }
38
49
  }
39
50
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instaserve",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Instant web stack",
5
5
  "main": "module.mjs",
6
6
  "bin": "./server.mjs",
@@ -9,8 +9,5 @@
9
9
  "deno": "deno run --unstable --allow-net --allow-read deno/server.js",
10
10
  "bun": "bun run bun/http.js",
11
11
  "test": "node --no-warnings test.mjs"
12
- },
13
- "dependencies": {
14
- "instax": "^0.1.2"
15
12
  }
16
13
  }
File without changes
package/public/index.html CHANGED
File without changes
package/public/sw.js CHANGED
File without changes
@@ -0,0 +1 @@
1
+ ok
package/routes.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  export default {
2
- _debug: ({method, url}, s) => !console.log(method, url),
3
- _example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
4
- api: () => 'an example api response'
5
- }
2
+ _debug: ({ method, url }, s) => !console.log(method, url),
3
+ _example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
4
+ api: () => 'an example api response'
5
+ }
package/test.mjs CHANGED
@@ -1,15 +1,44 @@
1
1
  import serve from './module.mjs'
2
2
  import { get, te, tde } from '../instax/module.mjs'
3
3
 
4
+ const port = 8080
4
5
  const server = serve({
5
6
  api: (r, s) => 'Hello!',
6
7
  api2: (r, s, data) => JSON.stringify(data)
7
- }, 3001)
8
+ }, port)
9
+ te(server.port, port)
8
10
 
9
- const resp = await get('http://localhost:3001/api')
11
+ // Routes
12
+ const resp = await get('http://localhost:8080/api')
10
13
  te(resp, 'Hello!')
11
- const resp2 = await get('http://localhost:3001/api2', {method: 'POST', body: JSON.stringify({a:1})})
14
+ const resp2 = await get('http://localhost:8080/api2', {method: 'POST', body: JSON.stringify({a:1})})
12
15
  tde(resp2, {a:1})
13
16
 
17
+ // Public
18
+ const testhtml = await get('http://localhost:8080/test.html')
19
+ te(testhtml, 'ok')
14
20
  te(server.stop(), true)
21
+
22
+ // Test route returned values
23
+ const db = {}
24
+ const server2 = serve({
25
+ _: ({url}) => console.log(url),
26
+ __: ({headers: {host}, method, url}) => console.log(host, method, url),
27
+ str: () => 'ok',
28
+ obj: x => ({a: 'ok'}),
29
+ undef: () => undefined
30
+ }, 8085)
31
+ te(server2.port, 8085)
32
+ te(server2.routes.str(), 'ok')
33
+
34
+ const return_str = await get('http://localhost:8085/str')
35
+ te(return_str, 'ok')
36
+
37
+ const return_obj = await get('http://localhost:8085/obj')
38
+ te(return_obj.a, 'ok')
39
+
40
+ const return_undefined = await get('http://localhost:8085/undef')
41
+ te(return_undefined, '')
42
+
43
+ server2.stop()
15
44
  console.log('tests complete')