简体   繁体   中英

Node.js Application with multiple endpoints

I have a node.js app that is just a library. No I would like to provide different endpoints for that application.

I would like to have a web page via express I would like to have an admin command line (And for future maybe an Rest Api.)

So how would one structure a node.js app to exposes a website and an Admin Command Line Interface. So that Admins can interact also with that application via command line at the same time. The point is to connect to the running process and interact with it (production save).

Since node is asynchronous, it has no problems listening to multiple endpoints simultaneously. The structure of the program is basically just creating two servers (HTTP/Express/Socket/...), configured separately, and calling listen() on both. As a quick and dirty example;

var http = require('http');
var net  = require('net');
var repl = require('repl');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!\n');
}).listen(8081);

net.createServer(function (socket) {
  repl.start({
    prompt: "node> ", input: socket, output: socket
  }).on('exit', function() {
    socket.end();
  });
}).listen(8082);

...will listen on port 8081/HTTP with a message, and a REPL via telnet to 8082.

You can naturally choose to reuse methods/configuration, but that's your choice, not anything enforced by Node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM