safe-run-middleware
v0.0.3
Published
This module uses the awesome run-middleware but wraps functions to ensure that routes called exist and that the correct method is used in calling the routes.
Downloads
9
Readme
#NodeJS safe-run-middleware
This module is an extension of run-middleware with the following key changes.
- It returns a promise
- It automatically ensures route to be called is already created via app or router and automatically adds the right method to call the route
Why?
Many times, your server and your client, need to execute the same functions. For example here is an endpoint to get user details:
app.get('/get-user/:id',function(req,res){
mysql.query('select * from users where id=?',[req.params.id],function(err,rows){
res.send({user:rows[0]})
})
})
Now you want to get the user details from your code. What should you do?
rc.runMiddleware('/get-user/20').then(function(code,body,headers){
console.log('User Details:',body)
})
Installation
yarn add safe-run-middleware
var express=require('express')
var app=express();
let rc = require('safe-run-middleware')(app)
Support & Contributions
- Pull requests, issues, and English proofreading are welcome on Github.
- Question & support on StackOverflow using
run-middleware
tag.
Change request paramaters
As options you can pass the query
, body
, method
, cookies
parameters.
rc.runMiddleware('/handler',{
method:'post',
query:{token:'tk-12345'},
body:{"action":"list","path":"/"}
}).then(function(code,data){
console.log(code,data)
process.exit()
})
Refer to Run Middleware
All other functionality is as documented at run middleware.