Best JavaScript code snippet using best
_server_test.js
Source:_server_test.js
1// Copyright (c) 2012 Titanium I.T. LLC. All rights reserved. See LICENSE.txt for details.2(function() {3 "use strict";4 var server = require("./server.js");5 var http = require("http");6 var fs = require("fs");7 var assert = require("assert");8 var CONTENT_DIR = "generated/test";9 var INDEX_PAGE = "index.html";10 var OTHER_PAGE = "other.html";11 var NOT_FOUND_PAGE = "test404.html";12 var INDEX_PAGE_DATA = "This is index page file";13 var OTHER_PAGE_DATA = "This is another page";14 var NOT_FOUND_DATA = "This is 404 page file";15 var PORT = 5020;16 var BASE_URL = "http://localhost:" + PORT;17 exports.setUp = function(done) {18 fs.writeFileSync(CONTENT_DIR + "/" + INDEX_PAGE, INDEX_PAGE_DATA);19 fs.writeFileSync(CONTENT_DIR + "/" + OTHER_PAGE, OTHER_PAGE_DATA);20 fs.writeFileSync(CONTENT_DIR + "/" + NOT_FOUND_PAGE, NOT_FOUND_DATA);21 done();22 };23 exports.tearDown = function(done) {24 cleanUpFile(CONTENT_DIR + "/" + INDEX_PAGE);25 cleanUpFile(CONTENT_DIR + "/" + OTHER_PAGE);26 cleanUpFile(CONTENT_DIR + "/" + NOT_FOUND_PAGE);27 done();28 };29 exports.test_servesFilesFromDirectory = function(test) {30 httpGet(BASE_URL + "/" + INDEX_PAGE, function(response, responseData) {31 test.equals(200, response.statusCode, "status code");32 test.equals(INDEX_PAGE_DATA, responseData, "response text");33 test.done();34 });35 };36 exports.test_supportsMultipleFiles = function(test) {37 httpGet(BASE_URL + "/" + OTHER_PAGE, function(response, responseData) {38 test.equals(200, response.statusCode, "status code");39 test.equals(OTHER_PAGE_DATA, responseData, "response text");40 test.done();41 });42 };43 exports.test_servesIndexDotHtmlWhenAskedForHomePage = function(test) {44 httpGet(BASE_URL, function(response, responseData) {45 test.equals(200, response.statusCode, "status code");46 test.equals(INDEX_PAGE_DATA, responseData, "response text");47 test.done();48 });49 };50 exports.test_returns404WhenFileDoesNotExist = function(test) {51 httpGet(BASE_URL + "/bargle", function(response, responseData) {52 test.equals(404, response.statusCode, "status code");53 test.equals(NOT_FOUND_DATA, responseData, "404 text");54 test.done();55 });56 };57 exports.test_requiresHomePageParameter = function(test) {58 test.throws(function() {59 server.start();60 });61 test.done();62 };63 exports.test_requires404PageParameter = function(test) {64 test.throws(function() {65 server.start(CONTENT_DIR);66 });67 test.done();68 };69 exports.test_requiresPortParameter = function(test) {70 test.throws(function() {71 server.start(CONTENT_DIR, NOT_FOUND_PAGE);72 });73 test.done();74 };75 exports.test_runsCallbackWhenStopCompletes = function(test) {76 server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT);77 server.stop(function() {78 test.done();79 });80 };81 exports.test_stopThrowsExceptionWhenNotRunning = function(test) {82 test.throws(function() {83 server.stop();84 });85 test.done();86 };87 function httpGet(url, callback) {88 server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT, function() {89 http.get(url, function(response) {90 var receivedData = "";91 response.setEncoding("utf8");92 response.on("data", function(chunk) {93 receivedData += chunk;94 });95 response.on("error", function(err) {96 console.log("ERROR", err);97 });98 response.on("end", function() {99 server.stop(function() {100 callback(response, receivedData);101 });102 });103 });104 });105 }106 function cleanUpFile(file) {107 if (fs.existsSync(file)) {108 fs.unlinkSync(file);109 assert.ok(!fs.existsSync(file), "could not delete test file: [" + file + "]");110 }111 }...
app.js
Source:app.js
1var content_dir = 'view/content/';23var app = angular.module('app', []);4app.config([ '$routeProvider', function($routeProvider) {5 $routeProvider.when('/people/:personId', {6 templateUrl : content_dir + 'people/person.html',7 controller : PeopleCtrl8 }).when('/photo', {9 templateUrl : content_dir + 'photo.html',10 controller : PeopleCtrl11 }).when('/news/details/:newsId', {12 templateUrl : content_dir + 'news/details.html',13 controller : NewsCtrl14 }).when('/team', {15 templateUrl : content_dir + 'people/team.html',16 }).when('/lab/equipments', {17 templateUrl : content_dir + 'lab/equipments.html',18 }).when('/lab/installations', {19 templateUrl : content_dir + 'lab/installations.html',20 }).when('/research/projects', {21 templateUrl : content_dir + 'research/projects.html',22 }).when('/research/areas', {23 templateUrl : content_dir + 'research/areas.html',24 }).when('/research/publications', {25 templateUrl : content_dir + 'research/publications.html',26 }).when('/resources', {27 templateUrl : content_dir + 'resources/resources.html',28 }).when('/contact', {29 templateUrl : content_dir + 'contact.html',30 }).when('/about', {31 templateUrl : content_dir + 'about.html',32 }).when('/links', {33 templateUrl : content_dir + 'links.html',34 }).when('/news', {35 templateUrl : content_dir + 'news/news.html',36 }).when('/', {37 templateUrl : content_dir + 'main.html',38 }).otherwise({39 redirectTo : '/'40 });41} ]);4243app.filter('range', function() {44 return function(input, min, max) {45 max = parseInt(max);46 for ( var i = min; i < max; i++) {47 input.push(i);48 }49 return input;50 };
...
Using AI Code Generation
1const CONTENT_DIR = "content";2const fs = require("fs");3const path = require("path");4const util = require("util");5const readdir = util.promisify(fs.readdir);6const stat = util.promisify(fs.stat);7const CONTENT_DIR = "content";8const fs = require("fs");9const path = require("path");10const util = require("util");11const readdir = util.promisify(fs.readdir);12const stat = util.promisify(fs.stat);13const CONTENT_DIR = "content";14const fs = require("fs");15const path = require("path");16const util = require("util");17const readdir = util.promisify(fs.readdir);18const stat = util.promisify(fs.stat);19const CONTENT_DIR = "content";20const fs = require("fs");21const path = require("path");22const util = require("util");23const readdir = util.promisify(fs.readdir);24const stat = util.promisify(fs.stat);25const CONTENT_DIR = "content";26const fs = require("fs");27const path = require("path");28const util = require("util");29const readdir = util.promisify(fs.readdir);30const stat = util.promisify(fs.stat);31const CONTENT_DIR = "content";32const fs = require("fs");33const path = require("path");34const util = require("util");35const readdir = util.promisify(fs.readdir);36const stat = util.promisify(fs.stat);37const CONTENT_DIR = "content";38const fs = require("fs");39const path = require("path");40const util = require("util");41const readdir = util.promisify(fs.readdir);42const stat = util.promisify(fs.stat);43const CONTENT_DIR = "content";44const fs = require("fs");45const path = require("path");46const util = require("util");47const readdir = util.promisify(fs.readdir);48const stat = util.promisify(fs.stat);49const CONTENT_DIR = "content";50const fs = require("fs");51const path = require("path");
Using AI Code Generation
1var CONTENT_DIR = "content";2var fs = require('fs');3var path = require('path');4var express = require('express');5var app = express();6app.use(express.static('public'));7app.get('/', function (req, res) {8 res.send('Hello World');9});10app.get('/index.html', function (req, res) {11 res.sendFile(path.join(__dirname, CONTENT_DIR, 'index.html'));12});13app.get('/process_get', function (req, res) {14 response = {15 };16 console.log(response);17 res.end(JSON.stringify(response));18});19app.get('/file_upload.html', function (req, res) {20 res.sendFile(path.join(__dirname, CONTENT_DIR, 'file_upload.html'));21});22var server = app.listen(8081, function () {23 var host = server.address().address;24 var port = server.address().port;25});26var CONTENT_DIR = "content";27var fs = require('fs');28var path = require('path');29var express = require('express');30var app = express();31app.use(express.static('public'));32app.get('/', function (req, res) {33 res.send('Hello World');34});35app.get('/index.html', function (req, res) {36 res.sendFile(path.join(__dirname, CONTENT_DIR, 'index.html'));37});38app.get('/process_get', function (req,
Using AI Code Generation
1const contentDir = path.join(process.cwd(), "content")2const fs = require("fs")3const { promisify } = require("util")4const readFile = promisify(fs.readFile)5const writeFile = promisify(fs.writeFile)6const mkdir = promisify(fs.mkdir)7const readdir = promisify(fs.readdir)8const stat = promisify(fs.stat)9const getFiles = async (dir) => {10 const subdirs = await readdir(dir)11 const files = await Promise.all(subdirs.map(async (subdir) => {12 const res = path.resolve(dir, subdir)13 return (await stat(res)).isDirectory() ? getFiles(res) : res14 }))15 return files.reduce((a, f) => a.concat(f), [])16}17const getFiles2 = async (dir) => {18 const subdirs = await readdir(dir)19 const files = await Promise.all(subdirs.map(async (subdir) => {20 const res = path.resolve(dir, subdir)21 return (await stat(res)).isDirectory() ? getFiles2(res) : res22 }))23 return files.reduce((a, f) => a.concat(f), [])24}25const getFiles3 = async (dir) => {26 const subdirs = await readdir(dir)27 const files = await Promise.all(subdirs.map(async (subdir) => {28 const res = path.resolve(dir, subdir)29 return (await stat(res)).isDirectory() ? getFiles3(res) : res30 }))31 return files.reduce((a, f) => a.concat(f), [])32}33const getFiles4 = async (dir) => {34 const subdirs = await readdir(dir)35 const files = await Promise.all(subdirs.map(async (subdir) => {36 const res = path.resolve(dir, subdir)37 return (await stat(res)).isDirectory() ? getFiles4(res) : res38 }))39 return files.reduce((a, f) => a.concat(f), [])40}41const getFiles5 = async (dir) => {42 const subdirs = await readdir(dir)43 const files = await Promise.all(subdirs.map(async (subdir) => {44 const res = path.resolve(dir, subdir)45 return (await stat(res)).isDirectory() ? getFiles5(res) : res46 }))47 return files.reduce((a, f) => a.concat(f), [])48}49const getFiles6 = async (dir) =>
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!!