DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Concurrency and Parallelism in Node.js for Scalable Apps
  • Streamline Microservices Development With Dapr and Amazon EKS

Trending

  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  1. DZone
  2. Coding
  3. JavaScript
  4. Simulate Server Responses With Node.js

Simulate Server Responses With Node.js

We cover to emulate any server behavior without changing a single line of code in the backend, all directly from the front-end.

By 
Roman Predein user avatar
Roman Predein
·
Dec. 04, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

A few days ago, while I was working on the front-end of a new project, I found myself in a situation where I needed to simulate different responses from the backend to check some functionalities and behavior in different browsers. This has encouraged me to write this articles about the Node.js simulate browser.

At this point, where a vast amount of companies are placing their bets on TDD, functional testing is a mere routine for the server side, but tables do turn on when it comes to the front-end development. With some promising tools in the game (Selenium comes off the top of my head), developers do prefer to leave some testing to ‘humans’ to perform. Why does this happen? Due to a lack of solid support community, or an insufficient number of documentation and guides, maybe. But this discussion is not to be held in this post.

Coming back to my original point, it could be an option to do some temporary quick changes in our backend, and that would be it. But what happens if we do not have the control of it? I mean, its development is owned by others, and all we do is work against a REST API. If that’s your case, here’s a solution that might help: a ‘dumb’ server whose behavior is controlled from the front-end.

To accomplish that, we will use Node.js with Express.js. We are going to build a server, whose response code and headers will be configured from a single POST call. Here’s a quick guide you can follow.

Getting Started With Node.js

The first thing we will need is to get Node.

To install it from the command line, run:

apt-get install -g nodejs 

Then, from our IDE of choice, create a new Node.js project, and install Express:

npm install express

And last, but not least, we will install something that will make our life much easier, bodyParser: 

npm install body-parser  

Basic Setup

Now we can start with our copycat server. This is a demo code you can find useful:

1

As you can see above, we will use 2 variables, to set the response code and headers, with default value: “200 OK” with no headers. Our server will be listening in port 8000.

Configuration Call

It’s about time to put some useful code in it. We will create a POST call that receives and saves the response code and an array of headers it will be answering with for now on. This route will be called from the front-end every time we need to change the behavior of our server.

2

In other words, we can set the response we want to get from the server, making a POST request to 127.0.0.1:8000/configure. Once we have done that, we will proceed with a new call, never mind its method or payload, to which the response will be the one we configured. The body this call expects is in JSON format:

{“headers”: {
“header 1″: “content”,
“header 2″: “content”,
…
},
“code”: “200”
}

*Note: in the ‘code’, add the 3 digits code you need. The above lines are just an example. 

Default Routing

Just one thing left: a default route that will use the variables we set in the configuration: 

3

This function will be executed for any route, as long as it is different from “POST/configure,” and the response will be the one stored in our global variables ‘code’ and ‘headers.’

That’s all we’re going to need. That means it’s about time to test it. In this particular case, I will be using Postman, a great Chrome extension, to perform the configuration POST call, and will try to see the default browser’s dialog that is displayed during Basic access authentication, displayed when the server’s response is ‘401 Unauthorized,’ with a header similar to: ‘WWW-Authenticate: Basic realm=something.’

First, we must run our server. Locate the .js file that contains our code and execute it in the command line:

node ourFileName.js 

Now, in Postman, we select the POST method, set the URL to 127.0.0.1:8000/configure, add the Content-Type header to application/json, and add a body that will be compatible with our copycat server, like so: 

{“headers”: {
“WWW-Authenticate”: “Basic realm=\”Custom Realm Name\””
},
“code”: “401”
}

Finally, open any browser and go to http://127.0.0.1:8000/ 

Results

4

5

The server response in the browser’s console.

To sum up, in this post we saw an easy way to improve our web development using some popular and well-documented tools, Node.js and Express.js.

We're now able to emulate any server behavior without changing a single line of code in the backend. In order to do so, we created our own server, which has a simple configuration, performed directly from the front-end.

Node.js

Published at DZone with permission of Roman Predein. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Concurrency and Parallelism in Node.js for Scalable Apps
  • Streamline Microservices Development With Dapr and Amazon EKS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!