Skip to content

Commit 13f7303

Browse files
committed
more node
1 parent 9719023 commit 13f7303

File tree

23 files changed

+724
-12
lines changed

23 files changed

+724
-12
lines changed

README.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21133,7 +21133,9 @@ Examples:
2113321133
* `class`
2113421134
** link:rootfs_overlay/lkmc/nodejs/object_to_string.js[]: `util.inspect.custom` and `toString` override experiment: https://stackoverflow.com/questions/24902061/is-there-an-repr-equivalent-for-javascript/26698403#26698403
2113521135
** link:rootfs_overlay/lkmc/nodejs/object_to_json.js[]: `toJSON` examples
21136+
** link:rootfs_overlay/lkmc/nodejs/static.js[]
2113621137
* link:rootfs_overlay/lkmc/nodejs/http.js[]: `http` module to create a simple HTTP server: https://nodejs.org/api/http.html
21138+
* link:rootfs_overlay/lkmc/nodejs/esm[]: https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module
2113721139

2113821140
===== Node.js step debugging
2113921141

rootfs_overlay/lkmc/nodejs/cjs/README.adoc

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
const assert = require('assert');
4+
5+
const notmain = require('./notmain.js')
6+
assert(notmain.x === 1)
7+
assert(notmain.y === 2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.x = 1;
2+
exports.y = 2;

rootfs_overlay/lkmc/nodejs/esm/README.adoc

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
3+
import { strict as assert } from 'assert';
4+
5+
import anyname from './notmain.js'
6+
assert(anyname === 1)
7+
8+
9+
// Not possible, have to use import everywhere.
10+
//import notmain2 from './notmain2.js'
11+
//const notmain2 = require('notmain2');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let a;
2+
export default a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {'notmain2': 2}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "esm",
3+
"version": "1.0.0",
4+
"type": "module"
5+
}

rootfs_overlay/lkmc/nodejs/express.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
#!/usr/bin/env node
22

33
const express = require('express')
4-
const app = express()
5-
const port = 3000
64

5+
const app = express()
76
app.get('/', (req, res) => {
8-
res.send('Hello World!')
7+
res.send('hello world')
8+
})
9+
app.get('/error', async (req, res, next) => {
10+
try {
11+
throw 'my error'
12+
res.send('never returned')
13+
} catch(error) {
14+
next(error);
15+
}
916
})
17+
const server = app.listen(3000, () => {
18+
console.log(`listening: http://localhost:${server.address().port}`)
1019

11-
app.listen(port, () => {
12-
console.log(`Example app listening at http://localhost:${port}`)
20+
// Test it.
21+
function test(path, method, status, body) {
22+
const assert = require('assert')
23+
const http = require('http')
24+
const options = {
25+
hostname: 'localhost',
26+
port: server.address().port,
27+
path: path,
28+
method: method,
29+
}
30+
http.request(options, res => {
31+
console.error(res.statusCode);
32+
assert(res.statusCode === status);
33+
res.on('data', d => {
34+
if (body !== undefined) {
35+
assert(d.toString() === body);
36+
}
37+
})
38+
}).end()
39+
}
40+
test('/', 'GET', 200, 'hello world')
41+
test('/', 'POST', 404)
42+
test('/dontexist', 'GET', 404)
43+
// Shows 'my error' on terminal, without stack trace.
44+
test('/error', 'GET', 500)
1345
})

0 commit comments

Comments
 (0)