tutorial socket node example espaƱol node.js express routes ports

node.js - socket - NodeJS Express-rutas separadas en dos puertos



socket.io example (2)

Basándome en la sugerencia de Explosion Pills anterior, modifiqué el código de esta manera:

var express = require(''express''); var things = []; var app = express(); var admin_app = express(); var port = 8080; var admin_port = 8081; app.post(''/factory/'', function(req, res) { //Create a thing and add it to the thing array }); //Assume more functions to do to things here.... admin_app.post(''/killallthings/'', function(req, res) { //Destroy all the things in the array }); admin_app.post(''/listallthings/'', function(req, res) { // Return a list of all the things }); admin_app.post(''/killserver/'', function(req,res){ //Kills the server after killing the things and doing clean up }); //Assume https options properly setup. var server = require(''https'').createServer(options, app); server.listen(port, function() { logger.writeLog(''Listening on port '' + port); }); var admin_server = require(''https'').createServer(options, admin_app); admin_server.listen(admin_port, function() { logger.writeLog(''Listening on admin port '' + admin_port); });

¡Ojalá supiera cómo dar el crédito a Explosion Pills por la respuesta! :)

Tengo un servidor Express y, mientras lo construía, creó varias funciones "auxiliares" en sus propias rutas. Me gustaría que esas rutas fueran accedidas en un puerto diferente. ¿Hay alguna forma de hacer esto en expreso?

En el código a continuación, la ruta "/ factory" (y otras funciones) estaría en un puerto, y las rutas auxiliares de "/ killallthings", "/ listallthings" y "/ killserver" estarían en un puerto separado.

Aquí hay una versión simplificada del código:

var express = require(''express''); var things = []; var app = express(); var port = 8080; app.post(''/factory/'', function(req, res) { //Create a thing and add it to the thing array }); //Assume more functions to do to things here.... app.post(''/killallthings/'', function(req, res) { //Destroy all the things in the array }); app.post(''/listallthings/'', function(req, res) { // Return a list of all the things }); app.post(''/killserver/'', function(req,res){ //Kills the server after killing the things and doing clean up }); //Assume https options properly setup. var server = require(''https'').createServer(options, app); server.listen(port, function() { logger.writeLog(''Listening on port '' + port); });

¿Es esto posible con express?


Si está intentando crear múltiples servidores, ¿por qué no crear múltiples archivos bin / www con diferentes puertos y configuraciones? Otra forma podría ser pasar el número de puerto directamente desde la línea de comando.