Best JavaScript code snippet using karma
WebServer.js
Source:WebServer.js
...14 case 401:15 serve401(res);16 break;17 case 404:18 serve404(res);19 break;20 default:21 serve500(res, err);22 break;23 }24 }25 26 ws.setupRoutes = function(exp){27 exp.get("/**.html", function(req, res){28 var filePath = path.join(__dirname, "html", req.url);29 if(req.url.indexOf("/modals") == 0){30 var modalStart = req.url.indexOf("/", 1) + 1;31 var modalEnd = req.url.indexOf("/", modalStart);32 var modal = req.url.substring(modalStart, modalEnd);33 filePath = path.join(__dirname, "modals", modal, "html", req.url.substring(req.url.lastIndexOf("/") + 1));34 }35 36 res.setHeader("Content-Type", "text/html; charset=utf8");37 res.status(200);38 if(!fs.existsSync(filePath)){39 serve404(res);40 }41 else {42 serveFile(filePath, res);43 }44 });45 exp.get("/**.css", function(req, res){46 var filePath = path.join(__dirname, "css", req.url);47 if(req.url.indexOf("/modals") == 0){48 var modalStart = req.url.indexOf("/", 1) + 1;49 var modalEnd = req.url.indexOf("/", modalStart);50 var modal = req.url.substring(modalStart, modalEnd);51 filePath = path.join(__dirname, "modals", modal, "css", req.url.substring(req.url.lastIndexOf("/") + 1));52 }53 54 res.setHeader("Content-Type", "text/css; charset=utf8");55 res.status(200);56 if(!fs.existsSync(filePath)){57 serve404(res);58 }59 else {60 serveFile(filePath, res);61 }62 });63 exp.get("/**.scss", function(req, res){64 var filePath = path.join(__dirname, "css", req.url);65 if(req.url.indexOf("/modals") == 0){66 var modalStart = req.url.indexOf("/", 1) + 1;67 var modalEnd = req.url.indexOf("/", modalStart);68 var modal = req.url.substring(modalStart, modalEnd);69 filePath = path.join(__dirname, "modals", modal, "css", req.url.substring(req.url.lastIndexOf("/") + 1));70 }71 72 res.setHeader("Content-Type", "text/css; charset=utf8");73 res.status(200);74 if(!fs.existsSync(filePath)){75 serve404(res);76 }77 else {78 sass.render({79 file: filePath,80 includePaths: [path.join(__dirname, "css", "base")]81 }, function(err, result){82 if(err){83 exp.serveError(500, err, res);84 }85 else {86 var css = result.css.toString();87 res.setHeader("Content-Length", css.length);88 res.end(css, "utf8");89 }90 });91 }92 });93 exp.get("/**.js", function(req, res){94 var filePath = path.join(__dirname, "js", req.url);95 if(req.url.indexOf("/modals") == 0){96 var modalStart = req.url.indexOf("/", 1) + 1;97 var modalEnd = req.url.indexOf("/", modalStart);98 var modal = req.url.substring(modalStart, modalEnd);99 var fileStart = req.url.lastIndexOf("/");100 var fileEnd = req.url.indexOf("?", fileStart);101 if(fileEnd == -1){102 fileEnd = req.url.length;103 }104 var fileName = req.url.substring(fileStart, fileEnd);105 filePath = path.join(__dirname, "modals", modal, "js", fileName);106 }107 else if(req.url.indexOf("?") > -1){108 filePath = path.join(__dirname, "js", req.url.substring(0, req.url.indexOf("?")));109 }110 111 res.setHeader("Content-Type", "application/javascript; charset=utf8");112 res.status(200);113 if(!fs.existsSync(filePath)){114 serve404(res);115 }116 else {117 serveFile(filePath, res);118 }119 });120 exp.get(["/**.png", "/**.jpg", "/**.jpeg", "/**.gif"], function(req, res){121 var filePath = path.join(__dirname, "images", req.url);122 123 if(req.url.substring(req.url.lastIndexOf(".")) == ".png"){124 res.setHeader("Content-Type", "image/png; charset=binary");125 }126 else if(req.url.substring(req.url.lastIndexOf(".")) == ".jpg" || req.url.substring(req.url.lastIndexOf(".")) == "jpeg"){127 res.setHeader("Content-Type", "image/jpeg; charset=binary");128 }129 else if(req.url.substring(req.url.lastIndexOf(".")) == ".gif"){130 res.setHeader("Content-Type", "image/gif; charset=binary");131 }132 res.status(200);133 if(!fs.existsSync(filePath)){134 serve404(res);135 }136 else {137 serveFile(filePath, res, "binary");138 }139 });140 }141 142 function serve401(res){143 res.status(401);144 res.setHeader("Content-Type", "text/html; charset=utf8");145 146 serveFile(path.join(__dirname, "html", "/errors/401.html"), res, "utf8");147 }148 149 function serve404(res){150 res.status(404);151 res.setHeader("Content-Type", "text/html; charset=utf8");152 153 serveFile(path.join(__dirname, "html", "/errors/404.html"), res, "utf8");154 }155 156 function serve500(res, err){157 res.status(500);158 res.setHeader("Content-Type", "text/html; charset=utf8");159 server.errorLog.error(err);160 161 if(res.req.url.indexOf(".html") > -1){162 serveFile(path.join(__dirname, "html", "/errors/500.html"), res, "utf8");163 }...
server2.js
Source:server2.js
...27 serveHTML("cats.html", response);28 break;29 default:30 // We don't recognize this URL -- serve a 404!31 serve404(response);32 }33 }34});35// We call on these help functions, giving each the response object (and filename in most cases) to serve the correctly-configred response.36// If any callback gets an error, meaning the file wasn't found, we serve 404!37function serveHTML(filename, response) {38 // Read a particular file...39 fs.readFile(`views/${filename}`, 'utf8', function (error, contents) {40 // Check to see if an error exists41 if (error) { return serve404(response) }42 // Respond to the browser43 response.writeHead(200, { 'Content-type': 'text/html' });44 response.write(contents);45 response.end();46 });47}48function serveCSS(filename, response) {49 // Read a particular file...50 fs.readFile(`stylesheets/${filename}`, 'utf8', function (error, contents) {51 // Check to see if an error exists52 if (error) { return serve404(response) }53 // Respond to the browser54 response.writeHead(200, { 'Content-type': 'text/css' });55 response.write(contents);56 response.end();57 });58}59function serveJPG(filename, response) {60 // Read a particular file...61 fs.readFile(`images/${filename}`, function (error, contents) {62 // Check to see if an error exists63 if (error) { return serve404(response) }64 // Respond to the browser65 response.writeHead(200, { 'Content-type': 'image/jpg' });66 response.write(contents);67 response.end();68 });69}70function serve404(response) {71 response.writeHead(404);72 response.end("File not found!");73}74server.listen(7077);...
common.js
Source:common.js
...37 return response.end(responseData)38 }39 return fs.readFile(filepath, function (error, data) {40 if (error) {41 return serve404(response, filepath)42 }43 if (!doNotCache) {44 cache[filepath] = data.toString()45 }46 response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain'))47 // call custom transform fn to transform the data48 responseData = transform && transform(data.toString()) || data49 response.writeHead(200)50 log.debug('serving: ' + filepath)51 return response.end(responseData)52 })53 }54}55var setNoCacheHeaders = function (response) {...
server.js
Source:server.js
...12 function type (t) { res.setHeader('Content-Type', t) }13 function resolve(file) { return path.join(__dirname, file) }14 function read(file) { return fs.createReadStream(resolve(file)); }15 function serve(file) { return read(file).on('error', serve404).pipe(res) }16 function serve404() { res.writeHead(404); res.end('Not found'); }17 // Pages18 if (req.url == '/' || req.url == '/index.html') {19 type('text/html')20 return serve('index.html')21 }22 if (req.url == '/whiteboard.html') {23 type('text/html')24 return serve('whiteboard.html')25 }26 // JS27 if (pathStarts('/js/')) {28 var b = browserify({ basedir: __dirname })29 b.add(resolve(path.basename(req.url)))30 b.ignore('proquint-')31 b.ignore('http')32 b.ignore('level')33 b.ignore('level/sublevel')34 b.ignore('level-sublevel/bytewise')35 b.ignore('pull-level')36 type('application/javascript')37 b.bundle()38 .on('error', function(err) {39 console.error(err.toString())40 serve404()41 })42 .pipe(res)43 return44 }45 // Static asset routes46 if (pathEnds('jpg')) type('image/jpeg')47 else if (pathEnds('jpeg')) type('image/jpeg')48 else if (pathEnds('gif')) type('image/gif')49 else if (pathEnds('ico')) type('image/x-icon');50 else if (pathEnds('png')) type('image/png');51 else if (pathEnds('woff')) type('application/x-font-woff')52 else if (pathEnds('woff2')) type('application/font-woff2')53 if (pathStarts('/img/') || pathStarts('/fonts/'))54 return serve(req.url)55 serve404();...
app.js
Source:app.js
...23 case "cats":24 serveHTML("cats.html", response);25 break;26 default:27 serve404(response);28 }29 }30});31function serveHTML(filename, response) {32 fs.readFile(`views/${filename}`, 'utf8', function(error, contents){33 if (error) { return serve404(response) }34 response.writeHead(200, {'Content-type' : 'text/html' });35 response.write(contents);36 response.end();37 });38}39function serveCSS(filename, response) {40 fs.readFile(`stylesheets/${filename}`, 'utf8', function(error, contents) {41 if (error) { return serve404(response) }42 response.writeHead(200, {'Content-type' : 'text/css' });43 response.write(contents);44 response.end();45 });46}47function serveJPG(filename, response) {48 fs.readFile(`images/${filename}`, function(error, contents) {49 if (error) { return serve404(response); }50 response.writeHead(200, {'Content-type' : 'image/jpg' });51 response.write(contents);52 response.end();53 });54}55function serve404(response){56 response.writeHead(404);57 response.end();58}59server.listen(7077);...
http-helpers.js
Source:http-helpers.js
...11 var url = req.pathname.replace(/^\//, "").replace(/http(s)?:\/\//, "").replace(/\/$/, "");12 //middleware.staticServe(__dirname+'/../archives/sites' + url, res, "", serve404);13 db.getHtml(url,function(err,rows){14 if(!!err || rows.length === 0){15 serve404(req,res);16 }else {17 if(rows[0].html){18 res.send(rows[0].html);19 } else {20 serve404(req, res);21 }22 }23 });24 }25};26var errorOccurred = function(req, res){27 res.status(500).send("Sorry an error occurred");28};29var serveIndex = function(req, res){30 middleware.staticServe(__dirname+'/public/index.html', res, "", errorOccurred);31};32var serveLoading = function(req, res){33 middleware.staticServe(__dirname+'/public/loading.html', res, "", errorOccurred);34};...
Using AI Code Generation
1var karma = require('karma');2var server = new karma.Server({3}, function(exitCode) {4 console.log('Karma has exited with ' + exitCode);5 process.exit(exitCode);6});7server.start();8"dependencies": {9 }
Using AI Code Generation
1var karma = require('karma');2var server = new karma.Server({3}, function(exitCode) {4 console.log('Karma has exited with ' + exitCode);5 process.exit(exitCode);6});7server.start();
Using AI Code Generation
1var karma = require('karma');2var karmaServer = new karma.Server({3}, function(exitCode) {4 console.log('Karma has exited with ' + exitCode);5 process.exit(exitCode);6});7karmaServer.start();8module.exports = function(config) {9 config.set({10 preprocessors: {},11 })12}
Using AI Code Generation
1var server = require('karma').server;2server.serve404('/some/url');3var server = require('karma').server;4server.serve404('/some/url');5var server = require('karma').server;6server.serve404('/some/url');7var server = require('karma').server;8server.serve404('/some/url');9var server = require('karma').server;10server.serve404('/some/url');11var server = require('karma').server;12server.serve404('/some/url');13var server = require('karma').server;14server.serve404('/some/url');15var server = require('karma').server;16server.serve404('/some/url');
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!!