Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:09+00:00 2026-06-17T09:05:09+00:00

Let’s say I am writing a web app with a Server and a Client.

  • 0

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:

  1. Run just the server (API) on some port (say 3000):

    my-app/server> node server.js
    ...Server listening on localhost:3000/api
    
  2. Run just the client (i.e. serve static files from /client directory):

    my-app/client> node client.js
    ...Server listening on localhost:4000/client
    
  3. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:05:09+00:00Added an answer on June 17, 2026 at 9:05 am

    You can instantiate a connect (the guts of express) server instance when starting both the server and the client from the same script and have it route the requests to node-static when the url starts with public and to connect otherwise.

    Something like

    var connect = require('connect');
    server = connect.createServer();
    server.use('/public', client.handler_function);
    server.use(server.express_handler_function);
    

    should do. You’ll need to expose the function(request, response) in client.js so that it can be referenced through client.handler_function and do the same for the express app (refer to the documentation).

    For example, esposing the function in client.js would involve something like:

    var static = require('node-static');
    var file = new(static.Server)('.');
    var handler = function (request, response) {
      request.addListener('end', function () {
        file.serve(request, response);
      });
    };
    var app = require('http').createServer(handler);
    if(module.parent){
       app.listen(4000);
       console.log("Server (static) listening on 4000")
    }
    module.exports = { handler_function: handler };
    

    So that you can get to handler by doing:

    var client = require('client'); // this returns the object you've set to `module.exports` in `client.js`
    [...]
    server.use('/public', client.handler_function);
    

    Other approaches

    What I’ve detailed above seems to be the closest to what you want (based on the clarification in your last edit). There are other options, though:

    • keep static and express-generated urls based at the site root, such as example.com/a_statically_served_script.js and example.com/api_endpoint; serving a static file is attempted first, if one cannot be found you’ll dispatch the request to the express-based app

    • use the app.js script to start both servers on different ports (or unix domain sockets) and use node-proxy (or something similar, or even nginx/apache as a reverse proxy) in front of them

    Same root

    For the first approach you need to add an error handler to file.serve such as

    file.serve(request, response, function(e, res) {
        if (e && (e.status == 404)) {
            // if the file wasn't found
            if (next) {
                next(request, response);
            }
        }
    }
    

    next should be a variable in the client.js script that is not set when the script is run directly but it is when the script is required (have a look at the documentation for how modules and exports in node work) – when set, next refers to a function that takes (req, res) and feeds them to express (have a look at the express docs on how to do this).

    Remarks

    Keep in mind this isn’t an exhaustive answer: it’s just a bunch of pointers on what documentation to look up and what techniques you could use to solve the problems.

    Something worth remembering is that more often than not in node a request handler is represented and implemented by a function(request, response). This idiom is extended in connect/express to funciton(request, response, next): here next represents the next avaliable handler (of the form function(request, response)) in the chain of handlers mounted to the server through server.use(handler).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the
Let me explain best with an example. Say you have node class that can
Let say I want to create an iOS app that download music files from
Let's say I have this element for displaying the website logo: <div id="web-title"> <a
Let me frame it this way.. Say I have an application server running on
Let's say I want to write an FTP client from scratch. In the command
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let's say I'm building an app that displays a UITableView of contacts. The user's
Let's say we're building a DB for storing analytics from web sites or mobile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.