express-annotator
v0.0.5
Published
Simple typescript annotations for ExpressJS routes
Downloads
12
Maintainers
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 settingsfalse
- 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 istrue
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