All PXE.js basic concepts.
The application is an object containing an array of middleware which are executed in sequence upon request.
A PXE.js hello world application:
const Server = require("@pxe/server");
const app = new Server();
app.use(async (ctx) => {
ctx.response.body = "Hello world!";
});
app.ls(3000);
PXE.js middlewares are basically Connect-style middlewares but asynchronous, which makes control flows easier.
The following example responses with an object with the time when the request started, the requested URL and method.
const Server = require("@pxe/server");
const app = new Server();
app.use(async (ctx, next) => {
await next();
ctx.response.body.url = ctx.request.url;
ctx.response.body.method = ctx.request.method;
});
app.use(async (ctx) => {
const time = new Date();
ctx.response.body = { time };
});
app.ls(3000);
The first middleware calls next()
, which runs the next middleware.
The next middleware set the response body to an object with property time
equals the current date.
The first middleware set the response body property url
to the requested URL and property method
to requested method.
The context is an object containing informations of the request and provides a way to manipulate the response. A default PXE.js context objects contains:
ctx.request
: All things about the requestctx.response
: The responsectx.cookie
: The current cookiectx.options
: Request options