Best JavaScript code snippet using mountebank
severTcp.js
Source:severTcp.js
1"use strict";2/**3 * Copyright (c) 2017, Yaacov Zamir <kobi.zamir@gmail.com>4 *5 * Permission to use, copy, modify, and/or distribute this software for any6 * purpose with or without fee is hereby granted, provided that the above7 * copyright notice and this permission notice appear in all copies.8 *9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16 */17var util = require("util");18var events = require("events");19var EventEmitter = events.EventEmitter || events;20var net = require("net");21var modbusSerialDebug = require("debug")("modbus-serial");22var HOST = "127.0.0.1";23var UNIT_ID = 255; // listen to all adresses24var MODBUS_PORT = 502;25// Not really its official length, but we parse UnitID as part of PDU26const MBAP_LEN = 6;27/* Get Handlers28 */29var handlers = require("modbus-serial/servers/servertcp_handler");30/* Add bit operation functions to Buffer31 */32require("modbus-serial/utils/buffer_bit")();33var crc16 = require("modbus-serial/utils/crc16");34/**35 * Helper function for sending debug objects.36 *37 * @param {string} text - text of message, an error or an action38 * @param {int} unitID - Id of the requesting unit39 * @param {int} functionCode - a modbus function code.40 * @param {Buffer} requestBuffer - request Buffer from client41 * @returns undefined42 * @private43 */44function _serverDebug(text, unitID, functionCode, responseBuffer) {45 // If no responseBuffer, then assume this is an error46 // o/w assume an action47 if (typeof responseBuffer === "undefined") {48 modbusSerialDebug({49 error: text,50 unitID: unitID,51 functionCode: functionCode52 });53 } else {54 modbusSerialDebug({55 action: text,56 unitID: unitID,57 functionCode: functionCode,58 responseBuffer: responseBuffer.toString("hex")59 });60 }61}62/**63 * Helper function for creating callback functions.64 *65 * @param {int} unitID - Id of the requesting unit66 * @param {int} functionCode - a modbus function code67 * @param {function} sockWriter - write buffer (or error) to tcp socket68 * @returns {function} - a callback function69 * @private70 */71function _callbackFactory(unitID, functionCode, sockWriter) {72 return function cb(err, responseBuffer) {73 var crc;74 // If we have an error.75 if (err) {76 var errorCode = 0x04; // slave device failure77 if (!isNaN(err.modbusErrorCode)) {78 errorCode = err.modbusErrorCode;79 }80 // Set an error response81 functionCode = parseInt(functionCode) | 0x80;82 responseBuffer = Buffer.alloc(3 + 2);83 responseBuffer.writeUInt8(errorCode, 2);84 _serverDebug("error processing response", unitID, functionCode);85 }86 // If we do not have a responseBuffer87 if (!responseBuffer) {88 _serverDebug("no response buffer", unitID, functionCode);89 return sockWriter(null, responseBuffer);90 }91 // add unit number and function code92 responseBuffer.writeUInt8(unitID, 0);93 responseBuffer.writeUInt8(functionCode, 1);94 // Add crc95 crc = crc16(responseBuffer.slice(0, -2));96 responseBuffer.writeUInt16LE(crc, responseBuffer.length - 2);97 // Call callback function98 _serverDebug("server response", unitID, functionCode, responseBuffer);99 // console.log("_callbackFactory",unitID,err, responseBuffer)100 return sockWriter(null, responseBuffer);101 };102}103/**104 * Parse a ModbusRTU buffer and return an answer buffer.105 *106 * @param {Buffer} requestBuffer - request Buffer from client107 * @param {object} vector - vector of functions for read and write108 * @param {function} callback - callback to be invoked passing {Buffer} response109 * @returns undefined110 * @private111 */112function _parseModbusBuffer(requestBuffer, vector, serverUnitIDS, sockWriter) {113 var cb;114 // console.log(123, requestBuffer)115 // Check requestBuffer length116 // if (!requestBuffer || requestBuffer.length < MBAP_LEN) {117 // modbusSerialDebug("wrong size of request Buffer " + requestBuffer.length);118 // return;119 // }120 var unitID = requestBuffer[0];121 // console.log(unitID)122 var functionCode = requestBuffer[1];123 var crc = requestBuffer[requestBuffer.length - 2] + requestBuffer[requestBuffer.length - 1] * 0x100;124 // if crc is bad, ignore message125 if (crc !== crc16(requestBuffer.slice(0, -2))) {126 modbusSerialDebug("wrong CRC of request Buffer");127 return;128 }129 // if crc is bad, ignore message130 if (!serverUnitIDS.includes(unitID) && !serverUnitIDS.includes(255)) {131 modbusSerialDebug("wrong unitID");132 return;133 }134 modbusSerialDebug("request for function code " + functionCode);135 cb = _callbackFactory(unitID, functionCode, sockWriter);136 switch (parseInt(functionCode)) {137 case 1:138 case 2:139 handlers.readCoilsOrInputDiscretes(requestBuffer, vector, unitID, cb, functionCode);140 break;141 case 3:142 handlers.readMultipleRegisters(requestBuffer, vector, unitID, cb);143 break;144 case 4:145 handlers.readInputRegisters(requestBuffer, vector, unitID, cb);146 break;147 case 5:148 handlers.writeCoil(requestBuffer, vector, unitID, cb);149 break;150 case 6:151 handlers.writeSingleRegister(requestBuffer, vector, unitID, cb);152 break;153 case 15:154 handlers.forceMultipleCoils(requestBuffer, vector, unitID, cb);155 break;156 case 16:157 handlers.writeMultipleRegisters(requestBuffer, vector, unitID, cb);158 break;159 case 43:160 handlers.handleMEI(requestBuffer, vector, unitID, cb);161 break;162 default:163 var errorCode = 0x01; // illegal function164 // set an error response165 functionCode = parseInt(functionCode) | 0x80;166 var responseBuffer = Buffer.alloc(3 + 2);167 responseBuffer.writeUInt8(errorCode, 2);168 // console.log("Illegal function", responseBuffer)169 modbusSerialDebug({170 error: "Illegal function",171 functionCode: functionCode172 });173 cb({modbusErrorCode: errorCode}, responseBuffer);174 }175}176/**177 * Class making ModbusTCP server.178 *179 * @param vector - vector of server functions (see examples/server.js)180 * @param options - server options (host (IP), port, debug (true/false), unitID)181 * @constructor182 */183var ServerTCP = function (vector, options) {184 var modbus = this;185 options = options || {};186 // create a tcp server187 // modbus._server = net.createServer();188 // modbus._server.listen({189 // port: options.port || MODBUS_PORT,190 // host: options.host || HOST191 // }, function() {192 // modbus.emit("initialized");193 // });194 // create a server unit id195 // var serverUnitID = options.unitID || UNIT_ID;196 var serverUnitIDS = options.unitIDS || [UNIT_ID];197 var clientKey = options.clientKey || "";198 var clientKeyBuf = Buffer.from(clientKey, "hex");199 // console.log(serverUnitIDS)200 // remember open sockets201 // modbus.socks = new Map();202 modbus.client = net.Socket();203 // è¿æ¥ tcp server204 modbus.client.connect(options, function () {205 console.log(clientKey, 'connected to Server');206 modbus.client.write(clientKeyBuf);207 })208 // æ¥æ¶æ°æ®209 modbus.client.on("data", function (data) {210 var recvBuffer = Buffer.from([]);211 modbusSerialDebug({action: "socket data", data: data});212 recvBuffer = Buffer.concat([recvBuffer, data], recvBuffer.length + data.length);213 // console.log("data", data, data.length)214 // console.log("recvBuffer", recvBuffer)215 console.log(clientKey, "receive", data)216 // Check the presence of the full request (MBAP + PDU)217 // remove mbap and add crc16218 var requestBuffer = Buffer.alloc(data.length);219 recvBuffer.copy(requestBuffer, 0, 0);220 // Move receive buffer on221 // recvBuffer = recvBuffer.slice(MBAP_LEN + pduLen);222 // var crc = crc16(requestBuffer.slice(0, -2));223 // requestBuffer.writeUInt16LE(crc, requestBuffer.length - 2);224 modbusSerialDebug({action: "receive", data: requestBuffer, requestBufferLength: requestBuffer.length});225 modbusSerialDebug(JSON.stringify({action: "receive", data: requestBuffer}));226 // console.log("requestBuffer", requestBuffer)227 var sockWriter = function (err, responseBuffer) {228 if (err) {229 modbus.emit("error", err);230 return;231 }232 // send data back233 if (responseBuffer) {234 // remove crc and add mbap235 // var outTcp = Buffer.alloc(responseBuffer.length + 6 - 2);236 // outTcp.writeUInt16BE(transactionsId, 0);237 // outTcp.writeUInt16BE(0, 2);238 // outTcp.writeUInt16BE(responseBuffer.length - 2, 4);239 // responseBuffer.copy(outTcp, 6);240 //241 // modbusSerialDebug({action: "send", data: responseBuffer});242 // modbusSerialDebug(JSON.stringify({action: "send string", data: responseBuffer}));243 // write to port244 modbus.client.write(responseBuffer);245 console.log(clientKey, "res", responseBuffer)246 }247 };248 // parse the modbusRTU buffer249 setTimeout(250 _parseModbusBuffer.bind(this,251 requestBuffer,252 vector,253 serverUnitIDS,254 sockWriter255 ),256 0257 );258 });259 modbus.client.on("error", function (err) {260 modbusSerialDebug(JSON.stringify({action: "socket error", data: err}));261 modbus.emit("socketError", err);262 });263 modbus.client.once("end", function () {264 modbusSerialDebug({265 action: "closed"266 });267 console.log(clientKey, "end")268 // modbus.socks.delete(sock);269 });270 EventEmitter.call(this);271};272util.inherits(ServerTCP, EventEmitter);273/**274 * Delegate the close server method to backend.275 *276 * @param callback277 */278ServerTCP.prototype.close = function (callback) {279 const modbus = this;280 // close the net port if exist281 if (modbus._server) {282 modbus._server.removeAllListeners("data");283 modbus._server.close(callback);284 // modbus.socks.forEach(function(e, sock) {285 // sock.destroy();286 // });287 modbusSerialDebug({action: "close server"});288 } else {289 modbusSerialDebug({action: "close server", warning: "server already closed"});290 }291};292/**293 * ServerTCP interface export.294 * @type {ServerTCP}295 */...
4.js
Source:4.js
1"use strict";2/**3 * Copyright (c) 2017, Yaacov Zamir <kobi.zamir@gmail.com>4 *5 * Permission to use, copy, modify, and/or distribute this software for any6 * purpose with or without fee is hereby granted, provided that the above7 * copyright notice and this permission notice appear in all copies.8 *9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16 */17var util = require("util");18var events = require("events");19var EventEmitter = events.EventEmitter || events;20var net = require("net");21var modbusSerialDebug = require("debug")("modbus-serial");22var HOST = "127.0.0.1";23var UNIT_ID = 255; // listen to all adresses24var MODBUS_PORT = 502;25// Not really its official length, but we parse UnitID as part of PDU26const MBAP_LEN = 6;27/* Get Handlers28 */29var handlers = require("./servertcp_handler");30/* Add bit operation functions to Buffer31 */32require("../utils/buffer_bit")();33var crc16 = require("../utils/crc16");34/**35 * Helper function for sending debug objects.36 *37 * @param {string} text - text of message, an error or an action38 * @param {int} unitID - Id of the requesting unit39 * @param {int} functionCode - a modbus function code.40 * @param {Buffer} requestBuffer - request Buffer from client41 * @returns undefined42 * @private43 */44function _serverDebug(text, unitID, functionCode, responseBuffer) {45 // If no responseBuffer, then assume this is an error46 // o/w assume an action47 if (typeof responseBuffer === "undefined") {48 modbusSerialDebug({49 error: text,50 unitID: unitID,51 functionCode: functionCode52 });53 } else {54 modbusSerialDebug({55 action: text,56 unitID: unitID,57 functionCode: functionCode,58 responseBuffer: responseBuffer.toString("hex")59 });60 }61}62/**63 * Helper function for creating callback functions.64 *65 * @param {int} unitID - Id of the requesting unit66 * @param {int} functionCode - a modbus function code67 * @param {function} sockWriter - write buffer (or error) to tcp socket68 * @returns {function} - a callback function69 * @private70 */71function _callbackFactory(unitID, functionCode, sockWriter) {72 return function cb(err, responseBuffer) {73 var crc;74 // If we have an error.75 if (err) {76 var errorCode = 0x04; // slave device failure77 if (!isNaN(err.modbusErrorCode)) {78 errorCode = err.modbusErrorCode;79 }80 // Set an error response81 functionCode = parseInt(functionCode) | 0x80;82 responseBuffer = Buffer.alloc(3 + 2);83 responseBuffer.writeUInt8(errorCode, 2);84 _serverDebug("error processing response", unitID, functionCode);85 }86 // If we do not have a responseBuffer87 if (!responseBuffer) {88 _serverDebug("no response buffer", unitID, functionCode);89 return sockWriter(null, responseBuffer);90 }91 // add unit number and function code92 responseBuffer.writeUInt8(unitID, 0);93 responseBuffer.writeUInt8(functionCode, 1);94 // Add crc95 crc = crc16(responseBuffer.slice(0, -2));96 responseBuffer.writeUInt16LE(crc, responseBuffer.length - 2);97 // Call callback function98 _serverDebug("server response", unitID, functionCode, responseBuffer);99 return sockWriter(null, responseBuffer);100 };101}102/**103 * Parse a ModbusRTU buffer and return an answer buffer.104 *105 * @param {Buffer} requestBuffer - request Buffer from client106 * @param {object} vector - vector of functions for read and write107 * @param {function} callback - callback to be invoked passing {Buffer} response108 * @returns undefined109 * @private110 */111function _parseModbusBuffer(requestBuffer, vector, serverUnitIDS, sockWriter) {112 var cb;113 console.log(123, requestBuffer)114 // Check requestBuffer length115 // if (!requestBuffer || requestBuffer.length < MBAP_LEN) {116 // modbusSerialDebug("wrong size of request Buffer " + requestBuffer.length);117 // return;118 // }119 var unitID = requestBuffer[0];120 console.log(unitID)121 var functionCode = requestBuffer[1];122 var crc = requestBuffer[requestBuffer.length - 2] + requestBuffer[requestBuffer.length - 1] * 0x100;123 // if crc is bad, ignore message124 if (crc !== crc16(requestBuffer.slice(0, -2))) {125 modbusSerialDebug("wrong CRC of request Buffer");126 return;127 }128 console.log(unitID)129 // if crc is bad, ignore message130 if (unitID !== 1) {131 modbusSerialDebug("wrong unitID");132 return;133 }134 modbusSerialDebug("request for function code " + functionCode);135 cb = _callbackFactory(unitID, functionCode, sockWriter);136 switch (parseInt(functionCode)) {137 case 1:138 case 2:139 handlers.readCoilsOrInputDiscretes(requestBuffer, vector, unitID, cb, functionCode);140 break;141 case 3:142 handlers.readMultipleRegisters(requestBuffer, vector, unitID, cb);143 break;144 case 4:145 handlers.readInputRegisters(requestBuffer, vector, unitID, cb);146 break;147 case 5:148 handlers.writeCoil(requestBuffer, vector, unitID, cb);149 break;150 case 6:151 handlers.writeSingleRegister(requestBuffer, vector, unitID, cb);152 break;153 case 15:154 handlers.forceMultipleCoils(requestBuffer, vector, unitID, cb);155 break;156 case 16:157 handlers.writeMultipleRegisters(requestBuffer, vector, unitID, cb);158 break;159 case 43:160 handlers.handleMEI(requestBuffer, vector, unitID, cb);161 break;162 default:163 var errorCode = 0x01; // illegal function164 // set an error response165 functionCode = parseInt(functionCode) | 0x80;166 var responseBuffer = Buffer.alloc(3 + 2);167 responseBuffer.writeUInt8(errorCode, 2);168 modbusSerialDebug({169 error: "Illegal function",170 functionCode: functionCode171 });172 cb({modbusErrorCode: errorCode}, responseBuffer);173 }174}175/**176 * Class making ModbusTCP server.177 *178 * @param vector - vector of server functions (see examples/server.js)179 * @param options - server options (host (IP), port, debug (true/false), unitID)180 * @constructor181 */182var ServerTCP = function (vector, options) {183 var modbus = this;184 options = options || {};185 // create a tcp server186 // modbus._server = net.createServer();187 // modbus._server.listen({188 // port: options.port || MODBUS_PORT,189 // host: options.host || HOST190 // }, function() {191 // modbus.emit("initialized");192 // });193 // create a server unit id194 // var serverUnitID = options.unitID || UNIT_ID;195 var serverUnitIDS = options.unitIDS || [UNIT_ID];196 console.log(serverUnitIDS)197 // remember open sockets198 // modbus.socks = new Map();199 modbus.client = net.Socket();200 // è¿æ¥ tcp server201 modbus.client.connect(options, function () {202 console.log('connected to Server');203 modbus.client.write('0004fffb');204 })205 // æ¥æ¶æ°æ®206 modbus.client.on("data", function (data) {207 var recvBuffer = Buffer.from([]);208 modbusSerialDebug({action: "socket data", data: data});209 recvBuffer = Buffer.concat([recvBuffer, data], recvBuffer.length + data.length);210 console.log("data", data, data.toString())211 console.log("recvBuffer", recvBuffer)212 // Check the presence of the full request (MBAP + PDU)213 // remove mbap and add crc16214 var requestBuffer = Buffer.alloc(data.length);215 recvBuffer.copy(requestBuffer, 0, 0);216 // Move receive buffer on217 // recvBuffer = recvBuffer.slice(MBAP_LEN + pduLen);218 var crc = crc16(requestBuffer.slice(0, -2));219 requestBuffer.writeUInt16LE(crc, requestBuffer.length - 2);220 modbusSerialDebug({action: "receive", data: requestBuffer, requestBufferLength: requestBuffer.length});221 modbusSerialDebug(JSON.stringify({action: "receive", data: requestBuffer}));222 console.log("requestBuffer", requestBuffer)223 var sockWriter = function (err, responseBuffer) {224 if (err) {225 modbus.emit("error", err);226 return;227 }228 // send data back229 if (responseBuffer) {230 // remove crc and add mbap231 var outTcp = Buffer.alloc(responseBuffer.length + 6 - 2);232 outTcp.writeUInt16BE(transactionsId, 0);233 outTcp.writeUInt16BE(0, 2);234 outTcp.writeUInt16BE(responseBuffer.length - 2, 4);235 responseBuffer.copy(outTcp, 6);236 modbusSerialDebug({action: "send", data: responseBuffer});237 modbusSerialDebug(JSON.stringify({action: "send string", data: responseBuffer}));238 // write to port239 modbus.client.write(outTcp);240 }241 };242 // parse the modbusRTU buffer243 setTimeout(244 _parseModbusBuffer.bind(this,245 requestBuffer,246 vector,247 serverUnitIDS,248 sockWriter249 ),250 0251 );252 });253 modbus.client.on("error", function (err) {254 modbusSerialDebug(JSON.stringify({action: "socket error", data: err}));255 modbus.emit("socketError", err);256 });257 modbus.client.once("end", function () {258 modbusSerialDebug({259 action: "closed"260 });261 console.log("end")262 // modbus.socks.delete(sock);263 });264 EventEmitter.call(this);265};266util.inherits(ServerTCP, EventEmitter);267/**268 * Delegate the close server method to backend.269 *270 * @param callback271 */272ServerTCP.prototype.close = function (callback) {273 const modbus = this;274 // close the net port if exist275 if (modbus._server) {276 modbus._server.removeAllListeners("data");277 modbus._server.close(callback);278 // modbus.socks.forEach(function(e, sock) {279 // sock.destroy();280 // });281 modbusSerialDebug({action: "close server"});282 } else {283 modbusSerialDebug({action: "close server", warning: "server already closed"});284 }285};286/**287 * ServerTCP interface export.288 * @type {ServerTCP}289 */...
sqlite.js
Source:sqlite.js
1const fs = require('fs');2const child_process = require('child_process');3module.exports = { connect };4function connect(sqlitePath, dbPath) {5 let sqlite = null;6 let requestBuffer = []7 let responseBuffer = [];8 let init = () => {9 sqlite = child_process.spawn(sqlitePath, [dbPath]);10 sqlite.stdout.on('data', data =>11 processResponse(sqlite, requestBuffer, responseBuffer, data.toString()));12 sqlite.on('close', () => {13 if (requestBuffer.length > 0) {14 let {onError, onSuccess} = requestBuffer.shift();15 onError('Connection closed');16 }17 init();18 });19 if (requestBuffer.length > 0)20 sendQuery(sqlite, requestBuffer, responseBuffer);21 };22 init();23 return {24 expr: (sql, ...params) => {25 return [sql, ...params]26 .map(x => x.toString().replace(/\n/g, ''))27 .join('\n');28 },29 query: (sql, onError, onSuccess) => {30 let qCount = sql.split('').reduce((acc, x) => acc += (x === '?' ) ? 1 : 0, 0);31 let lCount = sql.split('').reduce((acc, x) => acc += (x === '\n') ? 1 : 0, 0);32 if (qCount != lCount)33 return onError && onError('Malformed expression; mismatched parameters');34 requestBuffer.push({35 sql,36 onError: onError || (err => console.log(err)),37 onSuccess: onSuccess || (data => {}),38 });39 if (requestBuffer.length == 1)40 sendQuery(sqlite, requestBuffer, responseBuffer);41 },42 };43}44function sendQuery(sqlite, requestBuffer, responseBuffer) {45 if (requestBuffer.length == 0)46 return;47 responseBuffer.length = 0;48 let {sql, onError, onSuccess} = requestBuffer[0];49 sqlite.stdin.write(`${sql}\n`);50}51function processResponse(sqlite, requestBuffer, responseBuffer, str) {52 if (requestBuffer.length == 0)53 return;54 responseBuffer.push(str);55 56 let lastStr = responseBuffer[responseBuffer.length - 1];57 if (lastStr.slice(-6) === 'QUERY\n') {58 // End of response59 responseBuffer = responseBuffer60 .join('')61 .split('\n')62 .filter(x => x);63 let rows = [];64 let currentRow = {};65 for (let i = 0; i < responseBuffer.length; i++) {66 let line = responseBuffer[i];67 if (line === 'QUERY') {68 break;69 } else if (line === 'ROW') {70 rows.push(currentRow);71 currentRow = {};72 } else {73 let nextLine = responseBuffer[i + 1];74 currentRow[line] = (nextLine == '(null)') ? null : nextLine;75 i++;76 if (line === 'FAIL')77 break;78 }79 }80 let {onError, onSuccess} = requestBuffer[0];81 if (currentRow['FAIL']) {82 onError(currentRow['FAIL']);83 } else {84 onSuccess(rows);85 }86 // This has to be done directly before in case something gets added after the callback87 // There was a nasty race condition where `sendQuery` would be called twice per query,88 // once here and another time when the query was added because of the unsynchronized request buffer89 requestBuffer.shift();90 if (requestBuffer.length > 0)91 sendQuery(sqlite, requestBuffer, responseBuffer);92 }...
Using AI Code Generation
1var request = require('request');2var options = {3 headers: {4 },5 body: {6 {7 {8 is: {9 headers: {10 }11 }12 }13 }14 },15};16request(options, function (error, response, body) {17 if (error) throw new Error(error);18 console.log(body);19});20var request = require('request');21var options = {22 headers: {23 },24 body: {25 {26 {27 is: {28 headers: {29 }30 }31 }32 }33 },34};35request(options, function (error, response, body) {36 if (error) throw new Error(error);37 console.log(body);38});39var request = require('request');40var options = {41 headers: {42 },43 body: {44 {45 {46 is: {47 headers: {48 }49 }50 }51 }52 },53};54request(options, function (error, response, body) {55 if (error) throw new Error(error);56 console.log(body);57});
Using AI Code Generation
1var request = require('request');2var options = {3 headers: {4 },5 body: {6 {7 {8 equals: {9 }10 }11 {12 is: {13 headers: {14 },15 }16 }17 }18 },19};20request(options, function (error, response, body) {21 if (error) throw new Error(error);22 console.log(body);23});24var request = require('request');25var options = {26 headers: {27 }28};29request(options, function (error, response, body) {30 if (error) throw new Error(error);31 console.log(body);32});33var request = require('request');34var options = {35 headers: {36 }37};38request(options, function (error, response, body) {39 if (error) throw new Error(error);40 console.log(body);41});42var request = require('request');43var options = {44 headers: {45 }46};47request(options, function (error, response, body) {48 if (error) throw new Error(error);49 console.log(body);50});
Using AI Code Generation
1var request = require('request');2var options = {3 json: {4 {5 {6 equals: {7 }8 }9 {10 is: {11 headers: {12 },13 }14 }15 }16 }17};18request(options, function (error, response, body) {19 if (!error && response.statusCode == 201) {20 console.log(body);21 }22});23var request = require('request');24var options = {25 json: {26 {27 {28 equals: {29 }30 }31 {32 is: {33 headers: {34 },35 }36 }37 }38 }39};40request(options, function (error, response, body) {41 if (!error && response.statusCode == 201) {42 console.log(body);43 }44});45var request = require('request');46var options = {47 json: {48 {49 {50 equals: {51 }52 }53 {54 is: {55 headers: {56 },57 }58 }59 }60 }61};62request(options, function (error, response, body
Using AI Code Generation
1var request = require('request');2var options = {3 headers: {4 }5};6request(options, function (error, response, body) {7 if (!error && response.statusCode == 200) {8 console.log(body);9 }10 else {11 console.log(error);12 }13});
Using AI Code Generation
1var request = require('request');2var options = {3};4request(options, function (error, response, body) {5 if (!error && response.statusCode == 200) {6 }7});8var request = require('request');9var options = {10};11request(options, function (error, response, body) {12 if (!error && response.statusCode == 200) {13 }14});15var request = require('request');16var options = {
Using AI Code Generation
1var request = require('request');2var options = {3};4request(options, function (error, response, body) {5 console.log(body);6});7var request = require('request');8var options = {9};10request(options, function (error, response, body) {11 console.log(body);12});13var request = require('request');14var options = {15};16request(options, function (error, response, body) {17 console.log(body);18});19var request = require('request');20var options = {21};22request(options, function (error, response, body) {23 console.log(body);24});25var request = require('request');26var options = {27};28request(options, function (error, response, body) {29 console.log(body);30});31var request = require('request');32var options = {33};34request(options, function (error, response, body) {35 console.log(body);36});37var request = require('request');38var options = {39};40request(options, function (error, response, body) {41 console.log(body);42});
Using AI Code Generation
1const request = require('request');2const options = {3 headers: {4 }5};6request(options, function (error, response, body) {7 if (!error && response.statusCode == 200) {8 console.log(body);9 }10});11const request = require('request');12const options = {13 headers: {14 }15};16request(options, function (error, response, body) {17 if (!error && response.statusCode == 200) {18 console.log(body);19 }20});21const request = require('request');22const options = {23 headers: {24 }25};26request(options, function (error, response, body) {27 if (!error && response.statusCode == 200) {28 console.log(body);29 }30});31const request = require('request');32const options = {33 headers: {34 }35};36request(options, function (error, response, body) {37 if (!error && response.statusCode == 200) {38 console.log(body);39 }40});41const request = require('request');42const options = {43 headers: {44 }45};46request(options, function (error, response, body) {47 if (!error && response.statusCode == 200) {48 console.log(body);49 }50});
Using AI Code Generation
1const request = require('request');2const options = {3 headers: {4 },5 json: {6 {7 {8 is: {9 }10 }11 }12 }13};14request(options, function (error, response, body) {15 if (error) {16 console.log(error);17 } else {18 console.log(response.statusCode);19 }20});21const request = require('request');22const options = {23 headers: {24 },25 json: {26 {27 {28 is: {29 }30 }31 }32 }33};34request(options, function (error, response, body) {35 if (error) {36 console.log(error);37 } else {38 console.log(response.statusCode);39 }40});
Using AI Code Generation
1var request = require('request');2var fs = require('fs');3var mb = require('mountebank');4var options = {5 config: {},6};7var mb = mb.create(options);8mb.start();9var options = {10 json: {11 stubs: [{12 responses: [{13 is: {14 headers: {15 },16 body: JSON.stringify({
Using AI Code Generation
1var request = require('request');2var requestBuffer = function (request, response) {3 var data = '';4 request.on('data', function (chunk) {5 data += chunk;6 });7 request.on('end', function () {8 response.writeHead(200, { 'Content-Type': 'application/json' });9 response.end(JSON.stringify({ data: data }));10 });11};12var server = require('http').createServer(requestBuffer);13server.listen(3000);14request({15 json: {16 {17 {18 is: {19 headers: {20 },21 }22 }23 }24 }25}, function (error, response, body) {26 console.log('POST /imposters response', response.statusCode);27 console.log('POST /imposters response body', body);28 request({29 }, function (error, response, body) {30 console.log('GET /imposters/3000/requests response', response.statusCode);31 console.log('GET /imposters/3000/requests response body', body);32 request({33 }, function (error, response, body) {34 console.log('DELETE /imposters/3000 response', response.statusCode);35 console.log('DELETE /imposters/3000 response body', body);36 process.exit();37 });38 });39});40POST /imposters response body { port: 3000, protocol: 'http', numberOfRequests: 0 }41GET /imposters/3000/requests response body { requests:
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!