0.3.4 • Published 2 years ago

@web-alchemy/simple-server v0.3.4

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

Simple HTTP Server

Installation

npm install @web-alchemy/simple-server

Usage

const Application = require('@web-alchemy/simple-server');

const app = new Application();

app.on('GET /', ({ req, res }) => {
  res.end('/index');
});

app.on('POST /create/', ({ req, res }) => {
  res.end('/create/');
});

// special route for `404` handlers
app.on(Application.NOT_FOUND_ROUTE, (ctx) => {
  ctx.res.statusCode = 404;
  ctx.res.end('Not Found');
});

app.on('GET /some-error/', async ({ req, res }) => {
  throw new Error('boom');
});

app.on('error', ({ req, res, error }) => {
  res.end(error.message || 'error'); // boom (error from route `GET /some-error/`)
});

// works only if there is an handler `app.on('error', ctx => {})`
// https://nodejs.org/dist/latest-v14.x/docs/api/events.html#events_eventemitter_errormonitor
app.on(Application.errorMonitor, err => {
  console.log(err);
});

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
})
0.3.4

2 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago