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

xx-server

v1.0.3

Published

Default server for xerxes

Downloads

9

Readme

Install

npm install --save xx-server

Note This is a core package of xerxes, so usually you don't have to install it manually

Usage

In your app.js, put App.load('server/setup'); somewhere at the top. Optionally, but recommended, you can also include App.load('server/validations'); right after the setup (more on that below).

The server uses express, so you're probably already familiar with the syntax. It is exposed through App.router.

App.router.post('/users', function(req, res, next) {
  if (!req.body.email) {
    return next({ status: 422, code: 12345, errors: { email: [ 'email is missing' ] } });
  }
  res.setData({ user: { email: req.body.email } });
  next();
});

After you've defined the routes you have to start the server with App.load('server/start'); This will add a default error handler and 404 handler, so it's important to keep it at the end.

Conventions

One important difference to express is that you shouldn't directly end the request when you're done (with res.json(...) for example). Instead, you set the responseData of the response with res.setData({...}) and let the server figure the rest out by passing it further with next(). This way, other modules can still modify the response when they want to, which is an important key concept of xerxes.

There are also a few recommendations on the json data you return. When you for example return a user object, you should set the responseData to something like { user: { email: "[email protected]" } } instead of { email: "[email protected]" }. For endpoints that return lists, you would return { users: [ {...} ], meta: { page: 1, total: 300 } }. Error message should have the format as in the example above.

Nested routes

Let's say you have a module that creates CRUD endpoints on /comments (when doing App.load('comments/endpoints');), but you want them to be in /posts/:post_id/comments instead. Instead of modifying the comments module, you can simply do this:

App.router.push('/posts/:post_id', function() {
  App.load('comments/endpoints');
});

Validations

If you load server/validations, you'll be able to define what the data that gets passed to the endpoints looks like, by defining it through a json schema. Here is an example:

App.router.post('/users', App.router.describe({
  description: "Create a new *user*",
  properties: {
    email: { type: 'string' },
    info: {
      type: 'object',
      properties: { name: { type: 'string' }, birthyear: { type: 'integer' } }
      required: [ 'name' ]
    }
  }
  required: [ 'email', 'info' ]
}), function(req, res, next) {
  ...
});

You can read more about json schema here. This will not only give you validations, but there is the xx-docs package which generates an API documentation from these validations.

Rendering views

Although xerxes is primarily built to create APIs, it's also possible to specify a view with res.setView('index.html'). If the HTTP request accepts html, the server will respond with that html file, but if it's for example application/json then it will send the responseData as json. You can as well use a template engine. For example if you have ejs installed, you can do res.setView('index.ejs');. The data that you pass to res.setData will be used as template variables (locals).