I am new to programming and I need to create a restful web service using Restify but the entire application is build on Express.js . How to connect between the web service and the app?
Asked
Active
Viewed 274 times
0
-
you want to create a web service and a independent webapp?, or you want both to work as a 1 single app? – Sebastián Espinosa Feb 28 '16 at 21:31
-
Hell Sebastian, I want to create a web service and an independent webapp. Now I am having trouble fetching data from the webservice for the webapp – Murgin Boo Feb 28 '16 at 22:18
-
add some example code of the problem, to try to help you, maybe some error code or something, because your restify app creates endpoints and you need to make request to the endpoints from your express app. – Sebastián Espinosa Feb 29 '16 at 02:13
1 Answers
0
Restify and Express are both middleware modules (aka, in most cases you use one or the other, not both).
If you really need to...
In order to expose express routes with Restify, you would need to create your routes with Restify and then use some sort of http module to forward those requests to the express application. The NPM request Module would work.
Something like:
var restify = require('restify');
var request = require('request');
function respond(req, res, next) {
request.get('/url_of_express_app_endpoint', function(err, response, body) {
if (err) return next(err);
res.json(body);
});
}
var server = restify.createServer();
server.get('/hello/:name', respond);
However,
I strongly suggest just learning express and manipulating what you need in the express application, instead of maintaining TWO middleware applications (that sounds like a VERY unnecessary nightmare).

Olivercodes
- 1,048
- 5
- 17
-
Thanks for your answer. I was given the task to create a web service using restify and then consume the RESTful web service inside the app. That is why i am using two middlewares. – Murgin Boo Feb 29 '16 at 00:15