Files
linux-kernel-module-cheat/rootfs_overlay/lkmc/nodejs/express.js
Ciro Santilli 六四事件 法轮功 13f7303db1 more node
2021-05-25 01:00:00 +00:00

46 lines
1.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('hello world')
})
app.get('/error', async (req, res, next) => {
try {
throw 'my error'
res.send('never returned')
} catch(error) {
next(error);
}
})
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 => {
console.error(res.statusCode);
assert(res.statusCode === status);
res.on('data', d => {
if (body !== undefined) {
assert(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)
})