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

xweb

v1.1.1

Published

web xweb

Downloads

7

Readme

使用说明

  • 使用基本步骤请参考: koa-web
  • 新增session的支持,koa-session 数据保存到根目录的.cache中
  • koa-session默认保存到cookie中,这种情况并不安全
  • 但koa-session也提供了数据保存接口,xweb简单的封装了下
  • xweb启动时将自动清理过期的session数据文件

调试 xweb

npx xweb

无需安装访问:http://localhost:3000 就可以看到效果

安装 xweb

npm i xweb

启动程序

const Xweb = require("xweb");

const web = new Xweb();

// 应用于 koa-web的配置 app.use(KoaWeb({}));
// 参考: https://www.npmjs.com/package/koa-web
// 除 sessionKey 和 sessionMaxAge 外
web.config({
  // session 将保存到 __dirname/.cache中
  path: __dirname,

  // session文件保存路径
  // sessionPath: __dirname,

  // cookie键名 默认: xweb
  // sessionKey: "xweb",

  // cookie过期时间, 默认 86400000 (一天)
  // sessionMaxAge: 86400000
});

// // 调用于: koa.use
// web.use(async (ctx, next) => {
//   await next();
//   ctx.body = 'xweb';
// });

// 调用于: koa.listen
web.listen(3000, () => {
  console.log("server is running at http://localhost:3000");
});

session调用实例

创建html页面: /html/session/login.html

<html>
<head>
  <title>{% if login %}欢迎: {{ username }}{% else %}登录测试{% endif %}</title>
</head>
<body>
{% if login %}
  <div>欢迎: {{ username }}</div>
  <form action="login.logout" method="post">
    <div><input type="hidden" name="logout" value="true"></div>
    <div><input type="submit" value="退出"></div>
  </form>
{% else %}
  <form action="login.login" method="post">
    <div><input type="text" name="username"></div>
    <div><input type="text" name="password"></div>
    <div><input type="submit" value="登录"></div>
  </form>
{% endif %}
</body>
</html>

创建数据控制页面: /html/session/login.js

// html登录页面
module.exports.html = async hd => {
  if (hd.ctx.session.login) {
    hd.view({
      login: hd.ctx.session.login,
      username: hd.ctx.session.username
    });
  } else {
    hd.view({
      login: false
    });
  }
};

// 用户登录 账号: admin 密码: admin
module.exports.login = async (hd, data) => {
  if (!hd.isPost()) {
    return "数据提交错误!";
  }

  let username = data.username;
  let password = data.password;

  if (username !== 'admin' || password !== 'admin') {
    return "登录失败";
  }

  hd.ctx.session.login = true;
  hd.ctx.session.username = username;

  hd.ctx.status = 301;
  hd.ctx.redirect('/session/login');

  return "登录成功";
};

// 用户登出
module.exports.logout = async (hd, data) => {
  if (data.logout === 'true') {
    hd.ctx.session = null;
  }

  hd.ctx.status = 301;
  hd.ctx.redirect('/session/login');
};
  • 访问: http://localhost:3000/session/login