mojo.js

Real-time web framework for Node.js

README


undefined
Coverage Status
npm

The Mojolicious real-time web framework for Node.js. Written in
TypeScript. Meticulously designed for hypermedia-driven backend web services using all the latest JavaScript features.

If you want to stay up to date on the

Features


3x faster than Express.js and 15x faster than Mojolicious.
A real-time web framework, allowing you to easily grow single file prototypes into well-structured MVC web
  applications.
  Powerful out of the box with RESTful routes, WebSockets, plugins, commands, logging, templates, content negotiation,
    session management, form and JSON validation, testing framework, static file server, cluster mode, CGI detection,
    first class Unicode support and much more for you to discover.
A powerful web development toolkit, that you can use for all kinds of applications, independently of the web
  framework.
  High performance HTTP and WebSocket client/server implementation with support for HTTPS/WSS, cookies, redirects,
    urlencoded/multi-part forms, file uploads, JSON/YAML, HTML/XML, mocking, API testing, HTTP/SOCKS proxies, UNIX
    domain sockets and gzip compression.
  HTML/XML parser with CSS selector support.
No frontend framework, mojo.js is for the backend.
Written in TypeScript, with very cleanclass and async/await based APIs.
Very few dependencies, to avoid supply chain attacks and allow for "Perl-grade" long term support and
  backwards compatibility.
Fresh code based upon decades of experience developing Mojolicious and
  Catalyst, free and open source.

Installation


All you need is Node.js 16.0.0 (or newer).

  1. ```
  2. $ npm install @mojojs/core
  3. ```

Maybe take a look at our high quality spin-off projects @mojojs/dom,

Getting Started


  These four lines are a whole web application.

  1. ```js
  2. import mojo from '@mojojs/core';

  3. const app = mojo();

  4. app.get('/', ctx => ctx.render({text: 'I ♥ Mojo!'}));

  5. app.start();
  6. ```

  Use the built-in command system to start your web server.

  1. ```
  2. $ node index.mjs server
  3. [77264] Web application available at http://127.0.0.1:3000/
  4. ```

  Test it with any HTTP client you prefer.

  1. ```
  2. $ curl http://127.0.0.1:3000/
  3. I Mojo!
  4. ```

Duct Tape for the Web


  Use all the latest Node.js and HTML features in convenient single file prototypes like this one, and grow them easily
  into well-structured Model-View-Controller web applications.

  1. ```js
  2. import mojo from '@mojojs/core';

  3. const app = mojo();

  4. app.get('/', async ctx => {
  5.   await ctx.render({inline: inlineTemplate});
  6. });

  7. app.websocket('/title', ctx => {
  8.   ctx.plain(async ws => {
  9.     for await (const url of ws) {
  10.       const res   = await ctx.ua.get(url);
  11.       const html  = await res.html();
  12.       const title = html.at('title').text();
  13.       await ws.send(title);
  14.     }
  15.   });
  16. });

  17. app.start();

  18. const inlineTemplate = `
  19. <script>
  20.   const ws = new WebSocket('<%= ctx.urlFor('title') %>');
  21.   ws.onmessage = event => { document.body.innerHTML += event.data };
  22.   ws.onopen    = event => { ws.send('https://mojolicious.org') };
  23. </script>
  24. `;
  25. ```

Want to know more?


Take a look at our excellent documentation!