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

express-saga

v0.1.0

Published

express-saga is a web framework based on express and the saga pattern (redux-saga's interpretation).

Downloads

6

Readme

status: pre-alpha

express-saga is a web framework based on express and the saga pattern (redux-saga's interpretation).

You can use all existing express middleware without issue, but often there are better alternatives in express-saga.

Most effects from redux-saga are available, along with new effects for web server development.

This library is designed with a heavy focus on unit-testability. You don't need module level mocks due to dependency injection. Your routers are pure functions, so you don't need to run a http server in tests.

It also supports hot module replacement for far fewer in-development server restarts, without special build tools.

Install

npm install --save express express-saga

Example

First we'll set up a development server with hot-module-replacement. We're using mongo, but this will work with any database. This file only runs once, so create persistent objects here. When you change this file, restart the server.

server.js:

const express = requre('express');
const { MongoClient } = require('mongodb');
const hot = require('express-saga/hot');
const app = express();

function getSaga() {
  const saga = require('./rootSaga');
  return saga;
}

const mongo = new MongoClient();
mongo.connect(`mongodb://localhost:27017/myproject`).then((db) => {
  const services = { todos: db.collection('todos') };
  app.use(hot.saga(getSaga, services));
  app.listen(process.env.PORT || 4000);
});

Then rootSaga.js is the main app. We build and export a root saga which delegates work to the other sagas.

const $ = require('express-saga');

const staticSaga = function* (req, res, next) {
  const path = yield $.resolveStatic(`{__dirname}/public`, req.url);
  if (!path) return next();
  yield $.cors('*');
  yield $.send.file(path);
  yield $.end();
}

const getTodosSaga = function* () {
  const todoService = yield $.service('todos');

  // note that $.call takes a function; this aids in testing because
  // you can choose to call the function or not in tests
  const data = yield $.call(() => todoService.find({}).toArray());

  // send back as json, defaults to 200 status code
  yield $.send.json(data);
};

const createTodoSaga = function* () {
  const todoService = yield $.service('todos');

  // body parsing is done as-needed
  // this ensures you only accept the format you expect
  const body = yield $.body.json();
  try {
    yield $.call(() => todoService.create(body));
    yield $.send.json({ success: true });
  } catch (e) {
    yield $.status(400);
    yield $.send.json({ success: false });
  }
};

const apiRouter = function* () {
  yield $.get('/todos', getTodosSaga);
  yield $.post('/todos', createTodoSaga);
};

const rootSaga = function*(req, res, next) {
  const routes = [
    $.get('*', staticSaga),
    $.router('/api', apiRouter),
  ];

  yield $.routes(routes, req, res, next);
}

module.exports = rootSaga;

Then run node server.js, or env SAGA_DEBUG='*' node server.js.