takaya1992 / mojo.js

:unicorn: The Mojolicious real-time web framework for Node.js

Home Page:https://mojojs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm

The Mojolicious real-time web framework for Node.js. Written in TypeScript. If you want to stay up to date on the latest developments join us on IRC (#mojo.js on Libera.Chat).

Installation

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

$ npm install @mojojs/core

Getting Started

These four lines are a whole web application.

import mojo from '@mojojs/core';

const app = mojo();

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

app.start();

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

$ node index.mjs server
[77264] Web application available at http://127.0.0.1:3000/

Test it with any HTTP client you prefer.

$ curl http://127.0.0.1:3000/
I ♥ Mojo!

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.

import mojo from '@mojojs/core';

const app = mojo();

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

app.websocket('/title', ctx => {
  ctx.plain(async ws => {
    for await (const url of ws) {
      const res   = await ctx.ua.get(url);
      const html  = await res.html();
      const title = html('title').text();
      ws.send(title);
    }
  });
});

app.start();

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

Want to know more?

Take a look at our documentation!

About

:unicorn: The Mojolicious real-time web framework for Node.js

https://mojojs.org

License:MIT License


Languages

Language:JavaScript 61.1%Language:TypeScript 38.6%Language:Perl 0.2%Language:EJS 0.1%Language:Raku 0.1%