npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

warmup-nodejs

v1.0.5

Published

warmup tools for nodejs

Downloads

31

Readme

简介

nodejs服务的预热组件. 在nodejs服务启动之前预热代码, 减少nodejs代码编译对用户请求的影响. nodejs服务正常启动时, nodejs只会编译入口文件执行到的代码, 并不会编译未执行的代码. 像基于express/koa的web服务在服务启动时, 路由部分代码并不会运行到. 当用户请求到来时, nodejs才编译路由的代码, 造成较大耗时, 影响用户体验. 这个组件用来解决这个问题.

使用方法

使用下面命令安装这个组件:

npm install warmup-nodejs

或者

yarn add warmup-nodejs

概念介绍

本组件的预热请求支持get/post两种方法, 其中post支持表单和json两种方式. 预热请求任务包括三个参数, 分别是请求类型, 请求的路径, 数据. 当请求类型为get时, get请求的参数添加的请求的路径中. 当请求类型为post表单时, data字段是对应的表单数据. 当请求类型为post json数据时, data字段是对应json文件的路径.

基于对象的方法

假设服务的路由信息如下:

  • /get 支持get请求
  • /post 支持post 表单请求
  • /json 支持post json请求

实例代码如下:

import {Warmup, Task, METHOD} from "warmup-nodejs";
import express from "express";

const app = express();
const warmup = new Warmup(app, __dirname);
warmup.run([
    new Task(METHOD.GET, "/get?a=b"),
    new Task(METHOD.POST, "/post", {a: "b"}),
    new Task(METHOD.JSON, "/json", "data/a.json5")
]).then(value => {
    app.listen(3000, function () {
        console.log('Example app listening on port 3000!');
    });
}).catch(err => {
      console.log(err);
      process.exit(1);
  });

基于配置文件的方法

假设服务的路由信息如下:

  • /get 支持get请求
  • /post 支持post 表单请求
  • /json 支持post json请求

配置文件(conf/tasks.json5)内容如下:

{
  "taskList": [
    {
      "path": "/get?get=data",
      "method": "get"
    }, {
      "path": "/post",
      "method": "post",
      "data": {
        "post": "form"
      }
    }, {
      "path": "/json",
      "method": "json",
      "data": "data/demo.json"
    }
  ]
}

代码如下:

import express from "express";
import {WarmupProcess} from "warmup-nodejs";


const app = express();

const warmup = new WarmupProcess(__dirname);
warmup.run(app, "conf/tasks.json5").then(value => {
    console.log('success');
    app.listen(3000, function () {
        console.log('Example app listening on port 3000!');
    });
}).catch(err => {
    console.log(err);
});