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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:32:48+00:00 2026-06-11T02:32:48+00:00

Having recently developed an interest in NodeJS, I finally managed to get my hands

  • 0

Having recently developed an interest in NodeJS, I finally managed to get my hands on a NodeJS book. A simple example on the book was to create an express server and write a test to verify a simple API exposed as part of the exercise.

Please refer to the following code (I am using node v0.8.8)

The Server (nodeExample.js)

     var express = require('express')
     var app = express.createServer()
     app.listen(8000)
     var msgs = []

     app.get('/', function(req, res) {
        res.send('Welcome to Node Twitter')
     })

     app.post('/send', express.bodyParser(), function(req, res) {
        if (req.body && req.body.tweet) {
           msgs.push(req.body.tweet)
           res.send({status:"ok", message:"Message received"})
        } else {
            res.send({status:"nok", message:"Message not received"})
        }
    })

    app.get('/tweets', function(req,res) {
    res.send(msgs)
    })

The Test

var http = require('http'),
assert = require('assert')
var opts = {
  host: 'localhost',
  port: 8000,
  path: '/send',
  method: 'POST',
  headers: {'content-type':'application/x-www-form-urlencoded'}
}

var req = http.request(opts, function(res) {
 res.setEncoding('utf8')
 var data = "";
res.on('data', function(d) {
  data += d
})
res.on('end', function() {
  assert.strictEqual(data, {status:"ok", message:"Message received"})
 })
})

req.write('tweet=test')
req.end()

I managed to get the server up and running without issues ( Except for a warning that the createServer method of
express is deprecated , although doesn’t seem to be what’s bothering me ) ; but when I run the test, assert throws an exception indicating test failing, which should obviously not happen.

Here’s a screenshot of the problem

enter image description here

If you observe the error closely it seems to be the escape characters “\n” appended in parts of the response, that seem to be causing the problem.

I tried to circumvent the issue by

  1. trying different equality checks (equal , strictEqual and deepEqual)
  2. removing the utf8 encoding from the response

but had no luck ( unless I ofcourse perform an equality check with stringified test data, against the stringified response object returned where the formatting has been manually removed ).

So the question is,

  1. Why is the response formatted in the above manner ? Was it only for readability ?
  2. Assuming I am not allowed to remove the formatting, how can I perform an assert test on the response data I just received.
  • 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-11T02:32:50+00:00Added an answer on June 11, 2026 at 2:32 am

    The reply you get from your server is a JSON string:

    '{
        "status": "ok",
        "message": "Tweet received"
    }'
    

    You are comparing this to an object:

    {status:"ok", message:"Message received"}
    

    Since I gather you want to assert you get the correct reply, I suggest you JSON.parse the reply first, and compare the two objects with assert.deepEqual:

    assert.deepEqual(JSON.parse(data), {status:"ok", message:"Message received"})
    

    This is probably a wiser way than comparing a string to an object.

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

Sidebar

Related Questions

I've recently developed a WCF Facade service. I'm new to WCF and having trouble
We recently are having major performance related issues in our current SQL Server DB.
Having recently learned of the DebuggerDisplay attribute, I've found it quite useful. However, one
Having recently discovered design patterns, and having acquired the excellent Head First Design Patterns
Having recently seen a presentation of Clojure Protocols, I was quite impressed by the
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.
I've recently been having some issues with some tooltips on a webpage not displaying
I've recently started having trouble logging into my site using Facebook. I keep seeing
Having made the jump just recently from XEmacs to GNU Emacs, I'm really loving
Recently I was having some issues with a singelton class that was lazy initializing

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.