How to use addRoute method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

router.js

Source: router.js Github

copy
1// XXX Maybe split route handlers into separate modules?
2// Could do some kind of fs.readdir and then require them all.
3
4var routes = require('routes')
5, Router = routes.Router
6, Route = routes.Route
7, router = new Router()
8, config = require('./config.js')
9
10
11module.exports = router
12
13// static stuff serves out of the static folder.
14var static = require('./routes/static.js')
15router.addRoute('/static/*?', static)
16router.addRoute('/favicon.ico', static)
17router.addRoute('/install.sh', static)
18router.addRoute('/ca.crt', static)
19router.addRoute('/api/*?', static)
20router.addRoute('/api', static)
21// st should really just let you do either index.html OR autoindex
22router.addRoute('/doc/?', function (req, res) {
23  req.url = require('url').parse(req.url).pathname
24  if (!req.url.match(/\/$/)) return res.redirect(req.url+'/')
25  req.url += 'index.html'
26  static(req, res)
27})
28router.addRoute('/doc/*', static)
29
30router.addRoute('/stylus/*?', require('./routes/stylus.js'))
31// legacy
32router.addRoute('/dist/*?', distRedirect)
33router.addRoute('/dist', distRedirect)
34function distRedirect (req, res) {
35  var s = req.splats && req.splats[0] || ''
36  return res.redirect('http://nodejs.org/dist/npm/' + s, 301)
37}
38
39router.addRoute('/search', require('./routes/search.js'))
40
41router.addRoute('/login', require('./routes/login.js'))
42
43router.addRoute('/profile-edit', require('./routes/profile-edit.js'))
44
45router.addRoute('/email-edit', require('./routes/email-edit.js'))
46router.addRoute('/email-edit/:action/:token', require('./routes/email-edit.js'))
47
48router.addRoute('/profile/:name', profRedir)
49router.addRoute('/profile', profRedir)
50router.addRoute('/~/:name', profRedir)
51function profRedir (req, res) {
52  var n = req.params && req.params.name || ''
53  res.redirect('/~' + n, 301)
54}
55router.addRoute('/~:name', require('./routes/profile.js'))
56router.addRoute('/~', require('./routes/profile.js'))
57
58
59router.addRoute('/session', require('./routes/session-dump.js'))
60var logout = require('./routes/logout.js')
61router.addRoute('/logout', logout)
62router.addRoute('/logout/*?', logout)
63router.addRoute('/password', require('./routes/password.js'))
64router.addRoute('/signup', require('./routes/signup.js'))
65
66var forgot = require('./routes/forgot-password.js')
67router.addRoute('/forgot', forgot)
68router.addRoute('/forgot/:token', forgot)
69
70router.addRoute('/about', require('./routes/about.js'))
71
72router.addRoute('/', function (req, res) {
73  var search = req.url && req.url.split('?')[1]
74  if (search) return res.redirect('/search?' + search, 301)
75  return require('./routes/index.js')(req, res)
76})
77
78// The package details page
79var packagePage = require('./routes/package-page.js')
80router.addRoute('/package/:name/:version', packagePage)
81router.addRoute('/package/:name', packagePage)
82
83router.addRoute('/keyword/:kw', function (q, s) {
84  return s.redirect('/browse/keyword/' + q.params.kw, 301)
85})
86
87router.addRoute('/browse/*?', require('./routes/browse.js'))
88
89var ra = require('./routes/recentauthors.js') 
90router.addRoute('/recent-authors', ra)
91router.addRoute('/recent-authors/*?', ra)
92
93// npmjs.org/npm -> npmjs.org/package/npm
94// if nothing else matches.
95router.addRoute('/:name', require('./routes/maybe-package.js'))
96
Full Screen

sample.js

Source: sample.js Github

copy
1if (process.argv[1].indexOf('nodeunit') >= 0 || process.argv[1].indexOf('qnit') >= 0) return;
2
3require('qtimers')
4
5// sample Restiq app
6
7var cluster = require('cluster');
8var Restiq = require('../index');
9
10echoStack = [
11    //Restiq.mw.parseQueryParams,
12    //Restiq.mw.readBody,
13    //Restiq.mw.parseBodyParams,
14    //Restiq.mw.skipBody,
15    function(req, res, next) {
16        res.writeHeader(200, {'Content-Type': 'application/json'});
17        res.end(JSON.stringify(req.params));
18        next();
19    }
20];
21
22if (0 && cluster.isMaster) {
23    cluster.fork();
24    cluster.fork();
25    cluster.fork();
26//    cluster.fork();
27}
28else {
29    var app = Restiq.createServer({
30        //debug: 1,
31        //setNoDelay: true,
32        //readImmediate: 2,
33        //readBinary: true,
34    });
35    app.addStep(app.mw.parseQueryParams);
36    //app.pre(Restiq.mw.skipBody);
37    //app.pre(app.mw.readBody);
38    //app.pre(app.mw.parseBodyParams);
39    app.addRoute('GET', '/echo', echoStack);
40    app.addRoute('POST', '/echo', echoStack);
41    // 18.8k/s static routes w/ 5 url query params
42    // wrk -d20s -t2 -c8
43    // => 0.2.0: 19.1k/s (but 0.2.0 was buggy)
44    // => 0.3.0: 19.3k/s utf8, 18.2k/s binary 5 query params
45    // => 0.4.0: 19.6k/s utf8
46    // 20.5k/s static routes w/ 5 query params, skipped body (...why not closer to 27k/s?)
47/**
48app.addRoute('GET', '/:parm1/:parm2/echo1', echoStack);
49app.addRoute('GET', '/:parm1/:parm2/echo2', echoStack);
50app.addRoute('GET', '/:parm1/:parm2/echo3', echoStack);
51app.addRoute('GET', '/:parm1/:parm2/echo4', echoStack);
52app.addRoute('GET', '/:parm1/:parm2/echo5', echoStack);
53app.addRoute('GET', '/:parm1/:parm2/echo6', echoStack);
54app.addRoute('GET', '/:parm1/:parm2/echo7', echoStack);
55app.addRoute('GET', '/:parm1/:parm2/echo8', echoStack);
56app.addRoute('GET', '/:parm1/:parm2/echo9', echoStack);
57app.addRoute('GET', '/:parm1/:parm2/echo10', echoStack);
58app.addRoute('GET', '/:parm1/:parm2/echo11', echoStack);
59app.addRoute('GET', '/:parm1/:parm2/echo12', echoStack);
60app.addRoute('GET', '/:parm1/:parm2/echo13', echoStack);
61app.addRoute('GET', '/:parm1/:parm2/echo14', echoStack);
62app.addRoute('GET', '/:parm1/:parm2/echo15', echoStack);
63app.addRoute('GET', '/:parm1/:parm2/echo16', echoStack);
64app.addRoute('GET', '/:parm1/:parm2/echo17', echoStack);
65app.addRoute('GET', '/:parm1/:parm2/echo18', echoStack);
66app.addRoute('GET', '/:parm1/:parm2/echo19', echoStack);
67app.addRoute('GET', '/:parm1/:parm2/echo20', echoStack);
68**/
69    app.addRoute('GET', '/:parm1/:parm2/echo', echoStack);
70//    app.addRoute('POST', '/:parm1/:parm2/echo', echoStack);
71    // 20.3k/s parametric routes (2 parametric, no echo)
72    // => 0.5.0 21.7k/s 2 path params, no echo
73    // 17.7k/s 2 parametric + 5 echo
74    //app.addRoute('GET', '/echo2/:parm1/:parm2/:parm3/:parm4/:parm5', echoStack);
75    app.addRoute('GET', '/:parm1/:parm2/:parm3/:parm4/:parm5/echo', echoStack);
76    app.addRoute('POST', '/:parm1/:parm2/:parm3/:parm4/:parm5/echo', echoStack);
77    // 19.3k/s 5 path params w/o any query params
78    // 16.7k/s 5 path params + 5 query params
79    // => 0.3.0: 20.0k/s 5 path params w/o query params (strings)
80    // => 0.5.0: 17.3k/s 5 path params w 5 query params
81
82    app.listen(1337);
83    console.log("Server listening on 1337");
84}
85
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)