Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Aileron simplifies an API server to these steps:
URL pattern
strings to handler
functions.handler
returns a value (typically a JSON object), this is sent as a 200 response.handler
throws an error, this is sent as a 500 response.Additional features:
URL pattern
strings support :wildcards
, which are useful for specifying IDs in the URL string for example.handler
functions for different request methods (GET, POST, PUT, PATCH, DELETE).successHandler
and errHandler
functions to perform tasks like logging, specifying different status codes etc.inputs
to APIs. Aileron will check requests and reject incorrect inputs with a 409 response. This response can also be customized through a centralized badInputHandler
function.Middlewares:
middleware
.middleware
when you only want to match against the beginning of the URL, rather than exact matches.handler
function returns, we don't send a response, we call next()
, sending the request forward along the connect server chain.const connect = require("connect")
const aileron = require("aileron")
const { router, middleware } = aileron()
const teamDetails = {
get: {
errMsg: "Unable to retrieve team details"
handler: async (req, data) => {
const teamDetails = await getTeamDetails(data.teamId)
return { id: data.teamId, teamDetails }
}
},
put: {
errMsg: "Unable to update team details"
handler: async (req, data) => {
const result = await updateTeamDetails(data.teamId, data.teamList)
return result
}
}
}
const authMiddleware = {
errMsg: "Unauthorized request",
handler: (req, data) => {
const isAuthorized = await authorize(req)
}
}
let app = connect()
app
.use(middleware("/api/:apiVersion"), authMiddleware)
.use(router("/api/:apiVersion/team/:teamId", teamDetails))
router(urlFormat, routeConfig)
urlFormat
is a string URL, where you can have :wildcard
placeholders by prefixing a :
// urlFormatExample
"/api/:apiVersion/authenticate"
routeConfig
is an object containing a handler for each supported request method.
// route config example
const routeConfig = {
post: // Request method
{
inputs: // Input type definitions,
errMsg: // Error message string,
handler: (req, data) => {
// Function that returns a value.
// Returned value is passed to the successHandler which sends a response
// If an err is thrown, it is passed errHandler which sends a response
}
}
}
(req, data)
urlFormat
, the handler for the corresponding req.method
is called.next()
is called.inputs
it receives and their types. If inputs are missing / incorrect, aileron will automatically invoke badInputHandler
with a detailed error object. For advanced input validation, see the Input Checking
section.data
parameter. This will contain the wildcard values and the parsed inputs, ready for use.For example:
const loginApi = {
post: {
inputs: { username: "String", password: "String" },
errMsg: "Unable to login. Please try again!",
handler: async (req, data) => {
const userDetails = await loginUser(data.username, data.password, data.apiVersion)
return {message: "Login successful", userDetails}
}
}
}
let app = connect()
app
// Other routes and middleware
.use(...)
.use(...)
// The team route
.use(router("/api/:apiVersion/team/:teamId", teamApi))
// Other routes and middleware
.use(...)
.use(...)
apiVersion
) under which the variable is made available to the handler function.middleware(urlFormat, routeConfig)
Very similar to router, so we only explain the differences:
urlFormat
, the middleware function is called.handler
function returns, next()
is called.handler
function throws, the errHandler
is called.For example:
const printRequestInfo = (req, data) => {
console.log(req.method, req.url, data.apiVersion)
}
let app = connect()
app
.use(middleware("/api/:apiVersion", printVersionNumber))
// other middleware / routes follow
.use(...)
.use(...)
apiVersion
) under which the variable is made available to the middleware function.inputCheck
function. This function receives all the parsed inputs specified in your inputs
object. Simply throw an error inside this function and badInputHandler
will be called with the thrown error.?
to specify they're optional. {age: "Number | Undefined"}
can be written as {age: "Number?"}
const inputCheckingController = {
post: {
inputs: { name: "String", age: "Number" },
inputCheck: parsedInputs => {
// Custom check to disallow the name "Jon Snow"
if (parsedInputs.name === "Jon Snow") {
throw "You know nothing, Jon Snow"
}
},
errMsg: "Unable to process your request.",
handler: (req, res, next, data) => {
const { name, age } = data
res.ok().json({ name, age })
}
}
}
errHandler
and a badInputHandler
when you initialize Aileron.// MyCoolProject
const { router, middleware } = aileron({
badInputHandler: (req, res, err, errMsg) =>
res.forbidden().json({ err, message: "Bad Input: " + errMsg }),
errHandler: (req, res, err, errMsg) =>
res.error().json({ err, message: "Uncaught error!!" })
})
inputs
and errMsg
for all handlers.// MyCoolProject
const { router, middleware } = aileron({
strict: true,
badInputHandler: (req, res, err, errMsg) =>
res.forbidden().json({ err, message: "Bad Input: " + errMsg }),
errHandler: (req, res, err, errMsg) =>
res.error().json({ err, message: "Uncaught error!!" })
})
FAQs
Minimal URL matching for NodeJS
We found that aileron demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.