Socket
Socket
Sign inDemoInstall

urgot

Package Overview
Dependencies
40
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    urgot

a Node.js web framework


Version published
Weekly downloads
27
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Urgot 是一个轻量级高性能的 Node.js 框架,可以用TypeScript构建服务器端应用程序。
支持 http body 解析(x-www-form-urlencoded/form-data/binary)
支持 Session,基于 redis 等储存的会话信息,和JWT(JSON Web Token)的验证储存方式

安装

npm i urgot

Hello World

import { Server } from 'urgot'

const server = new Server()
server.use(async (context, next) => {
  context.body = 'Hello World'
  await next()
})
server.listen(5611)

调用 use 方法注册一个中间件,接受一个异步函数,当监听到请求时传入请求上下文(Context)和一个Next函数用于响应并执行下一个中间件

server.use(async (context, next) => {
  const start = Date.now()
  await next()
  const ms = Date.now() - start
  context.setHeader('X-Response-Time', `${ms}ms`)
  context.body = 'Hello World'
})

中间件使用洋葱模型,从请求(next()前)到响应(next()后),每一个中间件都有两次处理时机。

使用命令行创建项目

1.安装 typescript

npm i -g typescript

2.创建项目目录

mkdir myapp
cd myapp
npm i urgot

3.初始化项目和安装依赖

npx urgot init
npm i

4.运行开发环境

npx urgot dev

创建应用

调用 createApp 创建您的应用程序并传入您的应用和配置项,

import { App } from 'urgot'

class MyApp extends App {
  //实现
}

const server = new Server()
server.createApp([MyApp])
server.listen(5611)

Router 注解

使用 Router 注解去设置路由和HTTP方法

import { App, Server, Router } from 'urgot'

class MyApp extends App {
  @Router('GET')
  find() {
    const { searchParams } = this.context.url
    return { id: searchParams.get('id') }
  }
}

const server = new Server()
server.createApp([MyApp],{ onResponse: (vlaue, context) => context.body = vlaue as object })
server.listen(5611)

访问地址:http://localhost/?id=123456
页面响应:{ id: 123456 }

Router 不仅可以注解方法,还可注解控制器类来添加固定的路由前缀

import { App, Router } from 'urgot'

@Router('/video')
class MyApp extends App {
  @Router('GET','/list')
  find() {
    const { searchParams } = this.context.url
    return { targets: searchParams.getAll('name') }
  }
}

访问地址:http://localhost/video/list?name=Yone&name=Yasuo
页面响应: { tagrets: ["Yone", "Yasuo"] }

PS:Router 可以注册多次来定义多对一的路由

Use 注解

使用 Use 注解来注册自定义的拦截器

import { App, use, Router, Context } from 'urgot'

const auth = (context: Context) => {
  if (!context.url.searchParams.get('authed')) throw 'Please login'
}

@Router('/video')
class MyApp extends App {
  @Use(auth)
  @Router('GET','/list')
  find() {
    const { searchParams } = this.context.url
    return { targets: searchParams.getAll('name') }
  }
}

Parameter 注解

获取请求提交的参数是必不可少的一项,使用 Parameter 参数注解获取和转换用户提交的参数

import { App, Use, Router, Context } from 'urgot'

class Params {
  id!: number
  async onCreate() {
    const { searchParams } = this.context.url
    this.id = Number(searchParams.get('id'))
  }
}

@Router('/video')
class MyApp extends App {
  @Parameter(Params)
  @Router('GET','/list')
  find(params: Params) {
    return { upload: params.id }
  }
}

所有注解在抛出错误后都会阻止继续执行,并响应该错误值,利用这个特性来拦截一些操作。

命令行

可以使用 urgot 命令行来使用 urgot 的内置命令

命令说明示例
init初始化项目 (会覆盖当前文件夹)urtot init MyApp
dev运行开发环境urtot dev

常见问题

  • HTTP Body 解析
    调用context.request.body()解析请求Body,它返回一个Promise(注意多次调用仅在第一次解析,后续调用将返回缓存值
  • session 的使用
    要使用context.session需要在实例化Server时配置session实现参数,否则在调用时将抛出错误
const server = new Server({
  session: { 
    handlers: { 
      //实现方法,建议使用IORedis库,传递client即可
    }
  }
})
  • 处理路由函数返回的值
    你必须在调用 server.createApp()时传递 options选项来处理返回值
import { Server } from 'urgot'

const server = new Server()
server.createApp([],{
  //当路由方法返回时触发
  onResponse: (value, context) => {
    context.body = value as object
  }
})
server.listen(5611)

Keywords

FAQs

Last updated on 05 Apr 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc