Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node js using express and restify together in one app

I am using restify building apis, it works great. But I need to render some web pages as well in the same application.

Is it possible I can use express and restify together in one application?

this is the code for restify server in app.js

var restify = require('restify');
var mongoose = require('mongoose');


var server = restify.createServer({
    name : "api_app"
});

server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS());
mongoose.connect('mongodb://localhost/db_name');
server.get('/', routes.index);

server.post('/api_name', api.api_name);


server.listen(8000 ,"localhost", function(){
    console.log('%s listening at %s ', server.name , server.url);
});

how do I create express server in the same app.js?

Thanks

like image 785
wwli Avatar asked Dec 02 '13 05:12

wwli


People also ask

Why should you separate Express APP and server in NodeJS?

Applying a similar concept to the project structuring of Express, the separation of the application logic from the server allows the code to be modular and follow a MVC (Model-View-Controller) model. The separation is essential to reduce coupling and to encapsulate and abstract the inside logic of application.

Is Restify better than Express?

ExpressJS and Restify belong to "Microframeworks (Backend)" category of the tech stack. ExpressJS and Restify are both open source tools. It seems that ExpressJS with 44.6K GitHub stars and 7.48K forks on GitHub has more adoption than Restify with 9.31K GitHub stars and 957 GitHub forks.

Is ExpressJS and NodeJS same?

ExpressJS is a web application framework for NodeJS. That's what mainly makes the difference between Express JS and Node JS. The former provides various features that make web application development fast and easy, which otherwise takes more time using only the latter.


1 Answers

For all intents and purposes restify and express can't coexist in the same node process, because for unfortunate reasons, they both try to overwrite the prototype of the http request/response API, and you end up with unpredictable behavior of which one has done what. We can safely use the restify client in an express app, but not two servers.

like image 149
durgesh.patle Avatar answered Sep 29 '22 21:09

durgesh.patle