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-annotator

v0.0.5

Published

Simple typescript annotations for ExpressJS routes

Downloads

12

Readme

Express-annotator

installation

run command npm install express-annotator

javascript

create file server.js and write to it:

const Routes = require("express-annotator");
const app    = Routes.initApp({port: 3000});

@Routes.Handler()
class Greeting {
    @Routes.Get()
    onGreet(@Params.response() res, 
            @Params.query("name") userSurname = "World") {
        res.send("Hello " + userSurname);
    }
}
Routes.startApp()

run command node server.js

Visit localhost:3000 and you should see text Hello World.

Visit localhost:3000/?name=Pinocchio and you should see text Hello Pinocchio.

Typescript

create file server.ts and write to it:

import { Routes, Params } from "express-annotator";
import * as express from "express";

const app: express.Application = Routes.initApp({port: 3000});

@Routes.Handler()
class Greeting {
    @Routes.Get()
    public onGreet(@Params.response() res: express.Response, 
                   @Params.query("name") userSurname = "World"): void {
        res.send("Hello " + userSurname);
    }
}

run command tsc server.ts

run command node server.js

Visit localhost:3000 and you should see text Hello World.

Visit localhost:3000/?name=Pinocchio and you should see text Hello Pinocchio.

Route methods

Route

Basic method to create handler for specific route and method

Get

Annotation create simple get handler for route given as parameter

parameters
  • path - path to handler relative to parent paths. Default value is /

Post

Annotation create simple post handler for route given as parameter

parameters
  • path - path to handler relative to parent paths. Default value is /

Put

Annotation create simple put handler for route given as parameter

parameters
  • path - path to handler relative to parent paths. Default value is /

Delete

Annotation create simple delete handler for route given as parameter

parameters
  • path - path to handler relative to parent paths. Default value is /

SendFile

Method create handler which send content of specific file to client as response

parameters
  • fileName - name of file to send

Auth

Method turn on/off authentication for specific route. This method can also set authentication method.

parameters
  • authentication - set what method could be used for authentication. Default value is true
    • true - for authentication use global authentication settings
    • false - authentication for this method is switched off
    • (request: express.Request) => boolean - method that returns true if user has valid access rights otherwise returns false

Cookie

Method set cookie to client

parameters
  • key - cookie name
  • value - cookie value
  • options - advanced options for cookie
    • maxAge - maximal time for cookie
    • path - path where cookie will be stored

Redirect

Method redirect each request send to this route to path given as parameter.

parameters
  • destination - url to which the user is redirected
  • onlyRedirect - if true then ends after redirect otherwise continue to execute method. Default value is true

DownloadFile

Method send file for download to client

parameters
  • filePath - path to file on server

  • fileName - default filename used by system visible in download window

Parameter methods

query

To this parameter will contains value from query parameter. For example Params.query("name") will have after visit localhost:3000/?name=Pinocchio value Pinocchio

parameters
  • name - name of the query variable to be associated with the parameter variable

pathVariable

Path variables are stored in url just after the colon. For example Params.pathVariable("userName") will have after visit localhost:3000/userDetail/Pinocchio (route /userDetail:userName) value Pinocchio

parameters
  • name - name of the variable to be associated with the parameter variable

cookie

To parameter annotated by this method will have value of corresponding cookie.

parameters
  • name - name of the cookie to be associated with the variable

header

To parameter annotated by this method will have value of corresponding header.

parameters
  • name - name of the header to be associated with the variable

body

This annotation put in the following parameter request body

parameters
  • format - optional parameter use for parsing body. Allowed formats are JSON, XML, HTML, SVG

request

Annotate following parameter as express.Request object

response

Annotate following parameter as express.Response object