mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-25 19:21:35 +01:00
more node
This commit is contained in:
@@ -1,13 +1,45 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const express = require('express')
|
||||
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!')
|
||||
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}`)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening at http://localhost:${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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user