Node js using expression and regeneration together in one application

I use refify building apis, it works great. But I need to display some web pages in one application.

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

this is the code to restore the 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 to create an express server in the same app.js application?

thanks

+7
express restify
source share
5 answers

In all senses and tasks, regeneration and expression cannot coexist in the same node process, because for unfortunate reasons both of them try to rewrite the prototype of the HTTP request / response of the API, and you find yourself in unpredictable behavior from which one did what. We can safely use the restify client in an express application, but not on two servers.

+7
source share

I think recovery, like an expression, just creates a function that you can use as a request handler. Try something like this:

 var express = require('express'), restify = require('restify'), expressApp = express(), restifyApp = restify.createServer(); expressApp.use('/api', restifyApp); // use your restify server as a handler in express expressApp.get('/', homePage); expressApp.listen(8000); 
+3
source share

If you need REST APIs and regular web pages, I don't think you need to insist on this anymore. You can always simply use express and build your API on it instead of restraining, since express can do almost everything that needs to be done.

+1
source share

If you need to make a REST API and web application, I recommend using a framework that works in Express.

I created a rode framework that works great with REST and works with Express.

0
source share

I would solve this using the local api server and express proxy route. This may not be the best way with a little delay, but it is a possible solution on how to separate your web frame from your api.

0
source share

All Articles