Giter Club home page Giter Club logo

particula's Introduction

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.

particula's People

Contributors

samohovets avatar yamalight avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.