Using Parasite to Debug NodeJS applications - JavaScript in Plain English - Medi...
source link: https://medium.com/javascript-in-plain-english/using-parasite-to-debug-nodejs-applications-8c9445358708
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Using Parasite to Debug NodeJS applications
I’ve recently been investing a lot of time developing new features for Lynk and our most requested feature has been request capture and playback. This means being able to see the individual HTTP requests and responses going through the Lynk Tunnel (more information available here) but also being able to replay them as you change your code without having to go through the web browser each time.
This feature is especially useful when developing REST APIs since it allows you to quickly see how your application responds to various iterations of your code. Ngrok, the existing alternative to Lynk already offers this feature (including request playback) so it made sense to develop this for the upcoming beta release.
However, we didn’t want to keep this feature limited to Lynk. Having the ability to capture and replay requests through both a backend API and frontend UI makes sense for many applications — regardless of if they’re hosted through Lynk.
This is why it made sense to develop Parasite — a standalone proxy server which allows you to intercept, record, and replay incoming HTTP traffic. It’s also completely Open Sourced on our Github.
Parasite is also available as an NPM package and today I’ll walk you through embedding it in an ExpressJS API.
Setting up an ExpressJS project
For this tutorial we’ll set up a quick ExpressJS hello-world project that we can embed Parasite into. To do this, first make sure you have NodeJS v12.0.0 or later:
$ node --version
v12.0.0
And NPM v6.9.0 or later:
$ npm --version
6.9.0
Then start a hello-world ExpressJS project using the npx express generator
:
$ mkdir hello-world
$ cd hello-world
$ npx express-generator
This will create the necessary boilerplate for you. To start the server just run:
$ npm install
$ DEBUG=hello-world:* npm run start
Or with Yarn:
$ yarn install
$ DEBUG=hello-world:* yarn start
This will start a local development server on http://localhost:3000.
Close this when you’re done since we’ll be modifying the server to start Parasite inside it.
Installing Parasite
While Parasite is written in NodeJS, we’ve made the decision to also offer it as a packaged binary for environments where installing NodeJS is not possible.
For this tutorial, however, we’ll be installing the NPM package in our generated ExpressJS project:
$ npm install @loophole-labs/parasite
or if you prefer Yarn:
yarn add @loophole-labs/parasite
Now we need to modify the previously generated app.js
to include the following lines somewhere:
var Parasite = require('@loophole-labs/parasite');
var parasite = new Parasite();
Your final hello-world/app.js
should look like the following:
// hello-world/app.js
var Parasite = require('@loophole-labs/parasite');
var parasite = new Parasite();
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
And that’s it. Parasite is now embedded in your ExpressJS app.
Start the app back up with DEBUG=hello-world:* npm run start
or DEBUG=hello-world:* yarn start
and you’ll see Parasite create a proxy at http://localhost:8080 and forward traffic to your ExpressJS app (which is still running on port 3000).
You’ll also see the Web UI started at http://localhost:5050 and this is where all captured traffic will show up. You will also be able to replay requests from here:
As well as directly through the backend API available at http://localhost:5050/api.
Thanks for reading! Please visit our Documentation for more information, or contact me at [email protected]
. If you run into any problems open up an issue at https://github.com/loophole-labs/parasite.
You can find out more about me at https://shivanshvij.com
, and more about Lynk at https://lynk.sh.
We’re also beginning our closed of https://pigeonpost.io in the next few weeks, join our mailing list to stay informed!
A note from JavaScript In Plain English
We are always interested in helping to promote quality content. If you have an article that you would like to submit to JavaScript In Plain English, send us an email at [email protected] with your Medium username and we will get you added as a writer.
We have also launched three new publications! Show some love for our new publications by following them: AI in Plain English, UX in Plain English, Python in Plain English— thank you and keep learning!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK