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

particula

v0.10.2

Published

> Zero-config Express.js Framework

Downloads

43

Readme

Particula

Zero-config Express.js Framework

CircleCI npm license

Particula is a zero-config Express.js-based web framework.

⚠️Warning!⚠️

Particula is still pre-v1.0, API might be unstable and things might break along the way!

Project goals

The project has two simple goals:

  1. Use sensible defaults and best practices for Express.js out of the box
  2. Allow devs to focus on business logic, not framework setup

Features

  • POST/PUT/DELETE methods enabled by default
  • Helmet enabled by default
  • Async routes support out-of-the-box
  • Routes hot-reload (in development mode)
  • Full error stack traces on request (in development mode)

How to use

Setup

Install it:

npm install --save particula

and add a script to your package.json:

{
  "scripts": {
    "start": "particula"
  }
}

Now, your file-system is the main API. Every .js file becomes an Express route that gets automatically handled.

Populate ./routes/index.js inside your project:

const home = (req, res) => res.send('Welcome to particula.js!');

module.exports = home;

and then just execute npm start and go to http://localhost:8080.

POST, PUT, DELETE Routes

You might want to define routes that handle other types of requests than just GET.
To do that, simply export functions named after the method you want to use.
Here's a basic example of all possible handlers:

// routes/methods.js - /methods route will accept GET, POST, PUT, DELETE
// get handler
exports.get = (req, res) => res.send('i am normal get handler');
// post handler
exports.post = (req, res) => res.send({method: 'post', body: req.body});
// put handler
exports.put = (req, res) => res.send({method: 'put', body: req.body});
// delete handler
exports.delete = (req, res) => res.send('i am delete handler');

Custom Routes

Particula also provides a way to define custom routes.
This can be done by exporting named function useRouter.
Here's a basic example of handler with router:

// routes/subroutes.js - will use router so no prefix will be assigned
exports.useRouter = router => {
  router.get('/myCustomRoute', (req, res) => res.send({accepted: true, body: req.body}));
};

Middlewares

Particla provides a way to register global middlewares when needed.
To do so, simply create middlewares folder and add any .js files that export middleware handler inside.
Here's an example of basic middleware:

// middlewares/log.js - will be added as middleware to express instance
module.exports = (req, res, next) => {
  console.log('middleware triggered by:', req.originalUrl);
  next();
};

Plugins

In some cases you might want to have more complex scripts that interact with Express.js instance. This can be done via plugins.
Plugins must be registered using particula.config.js in the root of your application, like so:

// particula.config.js
const myPlugin = require('./myplugin');

module.exports = {
  plugins: [myPlugin],
};

Plugins have access to two function - setup and postsetup.
First - setup - function is executed before Particula does user middleware and routes setup.
While second - postsetup - function is executed after everything has been configured.
All data returned by setup function is stored using expressApp.set function and name variable exported by plugin.

Here's an example of simple plugin that adds passport-based authentication:

// auth.plugin.js
// defines the name of the plugin,
// the name will be used to store and access data returned from setup function
exports.name = 'auth-plugin';

exports.setup = app => {
  app.use(session());
  app.use(passport.initialize());
  app.use(passport.session());

  // passport setup
  // ...

  // renders login page
  app.get('/login', (req, res) => {});
  // handles auth with passport
  app.post('/login', passport.authenticate('local'));

  // function that checks if user is authenticated
  const isAuthed = (req, res) => {};

  return {isAuthed};
};

This plugin will exposed isAuthed function that can be accessed from anywhere in the following way:

// routes/protected.js
exports.useRouter = router => {
  const {isAuthed} = app.get('auth-plugin');
  router.get('/protected/me', isAuthed, (req, res) => res.send({accepted: true, body: req.body}));
};

License

Licensed under MIT.