Skip to content

Instantly share code, notes, and snippets.

@monochromer monochromer/app-example.js

Last active Jul 14, 2020
Embed
What would you like to do?
Веб-сервер на Node.js как итератор, как поток
const http = require('http');
const { once } = require('events');
class WebServer extends http.Server {
async *[Symbol.asyncIterator]() {
while (this.listening) {
const [ req, res ] = await once(this, 'request');
yield { req, res };
}
}
}
class Application {
middlewares = [];
server = null;
use(middleware) {
this.middlewares.push(middleware);
}
createHandler() {
return (ctx) => {
return this.middlewares.reduce((acc, fn) => {
return acc.then(() => fn(ctx))
}, Promise.resolve())
}
}
async run(...args) {
const server = this.server = new WebServer();
server.listen(...args);
const handler = this.createHandler();
for await (const ctx of server) {
handler(ctx)
.catch(error => {
ctx.res.statusCode = 500;
ctx.res.end(err.message);
});
}
}
}
const PORT = process.env.PORT || 3000;
const app = new Application();
app.use(async (ctx) => {
console.log('=== log ===');
console.log(ctx.req.url);
});
app.use(async (ctx) => {
ctx.state = ctx.state || {};
ctx.state.user = {
name: 'Alex',
age: 33
}
});
app.use(async (ctx) => {
ctx.res.setHeader('content-type', 'application/json');
ctx.res.end(JSON.stringify(ctx.state.user));
});
app.run(PORT, () => console.log(`Server is running on http://localhost:${PORT}`));
const http = require('http');
const { once } = require('events');
const { Readable } = require('stream');
const PORT = 3000;
class WebServer extends http.Server {
constructor() {
super(arguments);
}
async *[Symbol.asyncIterator]() {
while (this.listening) {
const [ req, res ] = await once(this, 'request');
yield {
req,
res
}
}
}
}
class StreamServer extends Readable {
constructor(options) {
super({
...options,
...{
objectMode: true
}
});
this._server = new WebServer;
this._server.on('close', () => {
this.push(null);
});
this._server.on('request', (req, res) => {
const ctx = { req, res }
if (!this.push(ctx)) {
this._server.close(() => {
console.log('Server is closed')
});
}
})
}
_read() {
if (!this._server.listening) {
this._server.listen(PORT, () => {
console.log(`Start on http://localhost:${PORT}`);
});
}
}
}
// server as iterator
// const server = new WebServer();
// server.listen(PORT, () => {
// console.log(`Start on http://localhost:${PORT}`);
// });
// (async () => {
// for await (let ctx of server) {
// ctx.res.end(JSON.stringify({
// url: ctx.req.url,
// headers: ctx.req.headers
// }))
// }
// })();
// server as stream
const server = new StreamServer();
server.on('data', ctx => {
ctx.res.end(JSON.stringify({
url: ctx.req.url,
headers: ctx.req.headers
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.