how can I map my set of url and middleware using loopback how can I map my set of url and middleware using loopback express express

how can I map my set of url and middleware using loopback


In loopback, you don't write get and post and other methods for a url as we do in express. You write the apis and expose different methods on them. Those apis will be accepting all the data required from request, process it with the help of datasource and return back.

Talking about custom response and middleware , you can register them in middlewarae.json file. For more you can refer to the documentation at this link :https://docs.strongloop.com/display/public/LB/Defining+middleware

As in the link states, strongloop has phases of routing a request.

LoopBack supports the following types of middleware:Pre-processing middleware for custom application logic. See example of static middleware. Dynamic request handling middleware to serve dynamically-generated responses, for example HTML pages rendered from templates and JSON responses to REST API requests. See example of pre-processing middleware.Static middleware to serve static client-side assets. See example of static middleware.Error-handling middleware to deal with request errors. See example of error-handling middleware.

EDIT : For this example you can refer to the following code snipped in particular :

var loopback = require('loopback');var bodyParser = require('body-parser'); var app = loopback(); app.middleware('routes:before', bodyParser.json());app.middleware('routes', loopback.rest());

More of you can still use your regular express type routing here in server.js file the you use it


Loopback is built on top of Express framework, and adding middleware in Loopback is not different from doing the same in Express.

Please check the Specifying routes section of this link. This link tells how can a middleware be registered in loopback. (There are other ways to register the middleware. This is one way.)

The code excerpts from the above link

For example, to register middleware for all endpoints that start with "/greet": In the file /server/server.js

var loopback = require('loopback');

var boot = require('loopback-boot');

var app = module.exports = loopback();

app.use('/greet', function(req, res, next ) { ... })

Now, if we modify the above code snippet to suit your case, you can use something like below:

var loopback = require('loopback');var boot = require('loopback-boot');var bodyParser = require('body-parser');var app = module.exports = loopback();app.use(bodyParser.json());app.get("/",function(req,res){....});app.post('/dataitsno', function(req,res){...});

Now, among the other ways of defining the middleware in loopback, one way can be placing your routes/middleware inside the boot folder where the scripts gets executed in lexicographically order at server boot time.

And the another one more way known to me should be using the middleware.json.