Let's say I am writing a web app with a Server and a Client.
- The server functions as an API, and uses express framework.
- The client is just a
node-static
app that serves static javascript/html files.
I want to be able to deploy them separately, independently of each other - or both at the same time.
Here is how I envision the directory structure:
/my-app
app.js
/server
server.js
/client
client.js
I would like to be able to run this in 3 different ways:
Run just the server (API) on some port (say 3000):
my-app/server> node server.js ...Server listening on localhost:3000/api
Run just the client (i.e. serve static files from /client directory):
my-app/client> node client.js ...Server listening on localhost:4000/client
Run both the server and the client, on the same port (by single node.js instance):
my-app> node app.js ...Server listening on localhost:5000
Is this possibe in node and what is the proper way to configure it?
I started as follows:
/////////////
// server.js
/////////////
// Run the server if this file is run as script
if(module.parent){
app.listen("3000/client")
}
/////////////
// client.js
/////////////
var static = require('node-static');
var file = new(static.Server)('.');
var app = require('http').createServer(function (request, response) {
request.addListener('end', function () {
file.serve(request, response);
});
});
if(module.parent){
app.listen("4000/client");
}
/////////////
// app.js
/////////////
server = require("server/server.js")
server.app.listen("5000/api")
client = require("client/client.js")
client.app.listen("5000/client") <--- ?????
I am not sure how to hook up both client and server inside my app.js so that they are both served from the same port/process/thread etc...
NOTE: Excuse the code, it is not tested and probably incorrect. I am new to node.js
Any tips appreciated.