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 8397483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:48:11+00:00 2026-06-09T20:48:11+00:00

Fairly new to Node.js Made an app that runs a server process and serve

  • 0

Fairly new to Node.js

Made an app that runs a server process and serve files (does not use express or any frameworks), Now I’m trying to unit test it.

I’m trying to use a mocha test for that… I intended to start my server process and then run requests against it to test the expected results (stats code, body content and the likes)

However it’s not working properly, all the request fail to connect to the server… I’m pretty sure that the issue is because node is juts running one process loop, the server is not running “in the background” while the queries run or possibly the server is not running yet (started ASYNC) while the request are being made ?

Anyway I was wondering what was the proper way to test this, I assume that either I need to have the server run in the background (like a forked process) and/or maybe I need to find a way to wait for the server process to be “up” first but not sure how.

Or at least recommendations on testing such server process (with Mocha or other).

Thanks.

Here is example test code (Updated since original question)

var server = new Server302('./fixture/');

var instance;

describe('Tests', function() {

before(function(done) {
     instance = http.createServer(function(request, response) {
        console.log(request.url);
        server.serve(request, response);
    }).listen(8000);
    instance.on("listening", function() {
        console.log("started");
        done();
    });
});

after(function(done){
  instance.close();
  console.log("stopped");
  done();
});

it("Should fetch test.html", function(done) {
    console.log("test1");
    http.get("http://localhost:8000/", function(res) {
        res.on('data', function(body) {
            console.log(body)
            expect(body).toEqual("test");
            done();
        });
    })
});

It seem to Execute in order but still fails with a connection error, whereas it works when testing manually with the browser:

started
test1
․․․stopped


  ✖ 1 of 1 tests failed:

  1) Tests Should fetch test.html:
  Error: connect ECONNREFUSED
  at errnoException (net.js:670:11)
  at Object.afterConnect [as oncomplete] (net.js:661:19)
  • 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-09T20:48:12+00:00Added an answer on June 9, 2026 at 8:48 pm

    In your before don’t call done until you get the “listening” event fired by the server.

    before(function(done) {
        instance = http.createServer(function(request, response) {
            console.log(request.url);
            server.serve(request, response);
        }).listen(8000);
        instance.on("listening", function() {
            console.log("started");
            done();
        });
    });
    

    That should ensure your test connections don’t start before the server is ready.

    See also the documentation for server.listen

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

Sidebar

Related Questions

I am new to node, npm, socket.io. Found many guides that are fairly useful,
Fairly new to Linq to XML How does one remove xml node ( recursively
I'm fairly new to node.js and I've found its quite complicated separating a project
fairly new iPhone developer here. Building an app to send RS232 commands to a
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...
I fairly new to JQuery and perhaps trying to achieve something that might be
I'm fairly new to ruby and I've got a hash that looks like so:
Fairly new to Google Maps API. I've got an array of data that I
I am fairly new to php and have not figured out how I would
I'm fairly new to node.js and am trying to ping my mysql database to

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.