📜  HoHo (1)

📅  最后修改于: 2023-12-03 15:15:32.396000             🧑  作者: Mango

HoHo - 一个快乐的编程框架 🎉

HoHo是一个基于Node.js的Web开发框架,旨在为开发者提供快捷、高效、可扩展的开发体验。HoHo的设计哲学是简洁、易用和高度可定制。

特性
  • 简单而强大的路由功能
  • 支持中间件机制,轻松实现功能扩展
  • 自动化多进程管理
  • 支持异步编程,采用Promise实现异步回调
  • 集成常见的数据库操作库,支持多种数据库
  • 支持插件扩展机制
安装

通过npm安装:

npm install hoho --save
基本用法

创建一个服务器,并监听在指定端口:

const HoHo = require('hoho');

const app = new HoHo();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});
路由

HoHo提供了简单而强大的路由设定功能:

const HoHo = require('hoho');

const app = new HoHo();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.post('/users', (req, res) => {
  // Create a new user
});

app.put('/users/:id', (req, res) => {
  // Update a user by id
});

app.delete('/users/:id', (req, res) => {
  // Delete a user by id
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});

路由参数可以通过req.params访问:

app.get('/users/:id', (req, res) => {
  const userId = req.params.id;
  // Query user info by userId
});
中间件

HoHo支持中间件机制,可以轻松地实现功能扩展:

const HoHo = require('hoho');

const app = new HoHo();

app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});

中间件可以通过next()调用链式执行:

app.use((req, res, next) => {
  console.log('middleware1');
  next();
});

app.use((req, res, next) => {
  console.log('middleware2');
  next();
});

app.get('/', (req, res) => {
  res.send('Hello, World!');
});
异步编程

HoHo采用Promise实现异步回调,可以实现更加简洁和易读的异步编程:

const HoHo = require('hoho');

const app = new HoHo();

app.get('/users/:id', (req, res) => {
  const userId = req.params.id;
  getUserById(userId)
    .then(user => {
      res.send(user);
    })
    .catch(error => {
      res.status(500).send(error);
    });
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});

function getUserById(id) {
  return new Promise((resolve, reject) => {
    // Query user info by id
    const user = { id, name: 'Alice' };
    setTimeout(() => {
      resolve(user);
    }, 1000);
  });
}
多进程管理

HoHo内置了自动化多进程管理功能,可以自动创建和管理多个子进程:

const HoHo = require('hoho');

const app = new HoHo({
  workers: 2,
});

app.get('/', (req, res) => {
  res.send(`Hello from worker ${process.pid}!`);
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});
数据库

HoHo集成了常见的数据库操作库,支持多种数据库:

const HoHo = require('hoho');
const { createPool } = require('mysql2/promise');

const app = new HoHo();

const pool = createPool({
  host: 'localhost',
  user: 'root',
  password: 'secret',
  database: 'test',
});

app.get('/users', async (req, res) => {
  try {
    const [rows, fields] = await pool.execute('SELECT * FROM users');
    res.send(rows);
  } catch (error) {
    res.status(500).send(error);
  }
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});
插件扩展

HoHo支持插件扩展机制,可以轻松地扩展功能:

const HoHo = require('hoho');

const app = new HoHo();

// Plugin definition
const myPlugin = {
  name: 'MyPlugin',
  version: '1.0.0',
  register: async (app, options) => {
    console.log('MyPlugin is running!');
  },
};

// Register plugin
app.register(myPlugin);

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});
完整示例
const HoHo = require('hoho');
const { createPool } = require('mysql2/promise');

const app = new HoHo({
  workers: 2,
});

const pool = createPool({
  host: 'localhost',
  user: 'root',
  password: 'secret',
  database: 'test',
});

app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

app.get('/', (req, res) => {
  res.send(`Hello from worker ${process.pid}!`);
});

app.get('/users', async (req, res) => {
  try {
    const [rows, fields] = await pool.execute('SELECT * FROM users');
    res.send(rows);
  } catch (error) {
    res.status(500).send(error);
  }
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000!');
});
结语

HoHo是一个简单而强大的编程框架,它提供了各种实用的功能和工具,可以帮助开发者快速创建高效可扩展的Web应用。如果你正在寻找一个快乐的编程框架,HoHo绝对是你的首选! 🎉