move nodejs/express to cirosantilli.github.io

This commit is contained in:
Ciro Santilli
2021-12-07 18:23:08 +00:00
parent 9b6552ab6c
commit 97773a4e3c
2 changed files with 1 additions and 87 deletions

View File

@ -1,86 +0,0 @@
#!/usr/bin/env node
const express = require('express')
function check_helper(req, res) {
if (req.params.param.length > 2) {
res.status(404)
res.send('ko')
} else {
return req.params.param + 'ok'
}
}
const app = express()
app.get('/', (req, res) => {
res.send('hello world')
})
app.get('/check-helper-1/:param', (req, res) => {
const ret = check_helper(req, res)
if (ret) {
res.send(ret)
}
})
app.get('/check-helper-2/:param', (req, res) => {
const ret = check_helper(req, res)
if (ret) {
res.send(ret)
}
})
app.get('/error', async (req, res, next) => {
try {
throw 'my error'
res.send('never returned')
} catch(error) {
next(error);
}
})
app.get('/query', (req, res) => {
res.send(`aa: ${req.query.aa} bb: ${req.query.bb}`)
})
app.get('/splat/:splat(*)', (req, res) => {
res.send('splat ' + req.params.splat)
})
const server = app.listen(3000, () => {
console.log(`listening: http://localhost:${server.address().port}`)
// Test it.
function test(path, method, status, body) {
const assert = require('assert')
const http = require('http')
const options = {
hostname: 'localhost',
port: server.address().port,
path: path,
method: method,
}
http.request(options, res => {
assert.strictEqual(res.statusCode, status);
res.on('data', d => {
if (body !== undefined) {
assert.strictEqual(d.toString(), body);
}
})
}).end()
}
test('/', 'GET', 200, 'hello world')
test('/', 'POST', 404)
test('/dontexist', 'GET', 404)
// Shows 'my error' on terminal, without stack trace.
test('/error', 'GET', 500)
test('/query?aa=000&bb=111', 'GET', 200, 'aa: 000 bb: 111')
// https://stackoverflow.com/questions/10020099/express-js-routing-optional-splat-param
// https://stackoverflow.com/questions/16829803/express-js-route-parameter-with-slashes
// https://stackoverflow.com/questions/34571784/how-to-use-parameters-containing-a-slash-character
test('/splat/aaa', 'GET', 200, 'splat aaa')
test('/splat/aaa/bbb', 'GET', 200, 'splat aaa/bbb')
test('/splat/aaa/bbb/ccc', 'GET', 200, 'splat aaa/bbb/ccc')
test('/check-helper-1/aa', 'GET', 200, 'aaok')
test('/check-helper-2/bb', 'GET', 200, 'bbok')
test('/check-helper-1/ccc', 'GET', 404, 'ko')
test('/check-helper-2/ddd', 'GET', 404, 'ko')
})

View File

@ -25,7 +25,7 @@ function generatePrimes(start, range) {
if (isMainThread) {
const max = 1e2;
const threadCount = +process.argv[2] || 10;
const threads = new Set();;
const threads = new Set();
const range = Math.ceil((max - min) / threadCount);
let start = min;
for (let i = 0; i < threadCount - 1; i++) {