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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:00:51+00:00 2026-06-13T08:00:51+00:00

I have a simple Node.js Rest server with a single POST service using Restify.

  • 0

I have a simple Node.js Rest server with a single POST service using Restify. I am trying to write a simple Mocha test, however it fails with a timeout, although succeeds with a REST Console test (browser plugin).

My server code:

/**
 * Module dependencies
 */

var restify = require('restify');
var events = require('events');
var util = require('util');


/**
 * Create App
 */

var server = restify.createServer({
  name: 'test',
    version: '0.0.1'
    });

var eventsEmitter = new events.EventEmitter();

/**
 * Configuraion
 */

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());


/**
 * Routes
 */


server.post('/post', function (req, res, next) {
  var text = "";
  req.setEncoding("utf8");

  req.on("data", function (chunk) {
    text += chunk;
  });
  req.on("end", function () {
    res.send(200, {ok: 'ok'});   
  });

  return next();
});


/**
 * Listen
 */ 

server.listen(8080, function () {
  console.log('%s listening at %s', server.name, server.url);
});

The Mocha test is as follows:

var restify = require('restify');
var assert = require('assert');

// init the test client
var client = restify.createJsonClient({
  url: 'http://127.0.0.1:8080',
  version: '*'
});

describe('service: post endpoint', function() {

// Test #1
describe('200 response check', function() {
    it('should get a 200 response', function(done) {
        client.post('/post', { hello: 'world' }, function(err, req, res, data) {                
            if (err) {
                throw new Error(err);
            }
            else {

                if (data.code != 200) {
                    throw new Error('invalid response from /post');
                }
                done();
            }
        });
    });
});

});

Can anyone advise why the test would timeout (I have tested with increasing the timeout in Mocha), but succeed through a browser?

  • 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-13T08:00:52+00:00Added an answer on June 13, 2026 at 8:00 am

    The problem is that Restify calls your server.post('/post', handler) function when the request is finished being received. You don’t have to wait for the data and end events. It is something that Restify (and other libraries like it, eg. Express) do for you. So all you need to do is write

    server.post('/post', function (req, res, next) {
        res.send(200, {ok: 'ok'});
    });
    

    And then you should not have a timeout. You were having the timeout because your handler was waiting for events that already came.

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

Sidebar

Related Questions

Okay, I have a simple test server set up using socket.io in node.js. My
I have another simple question about Node. I'm trying to make a simple http
I have simple node js http server. var http = require(http); http.createServer(function(request, response) {
I'm trying to build a simple web application using Node.js and CouchDb. The application
I have a simple single node cassandra setup (1.1.0) (default settings). Whenever I try
I have a simple webapp using node.js that reads the database and displays this
I have a simple TreePanel. I would like to select a particular node upon
I have this very simple C++ class: class Tree { public: Node *head; };
To keep things simple, lets say I have a Node class, each node has
I have this simple code of node addon: #include <node.h> #include <v8.h> #include <string>

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.