Routing in javascript

Asked 4 months ago, Updated 4 months ago, 7 views

If I concatenate about 30 js files and unify the js files, how do I implement the routing process on the client side?

Currently, we use page.js to determine the URL and run it.
I am considering whether there are other implementation methods.
But I can't think of anything.

By the way, we are not developing SPAs.
I'd like someone to give me some advice.
If there is not enough explanation, I will answer.

javascript

2022-09-30 11:30

1 Answers

At any rate, a router that meets certain criteria is essential.This allows you to control the execution of the script (function included in ) through the router, so there should be little impact on the configuration of the application code.

Even if you haven't developed a SPA, I think routing is a worthwhile approach.Build routes such as director...

var routes={
    '/script1': require('script1').hoge,
    '/script2': require('script2').hoge,
    '/books/view/:bookId':viewBook
  };

  var router = Router(routes);

  //
  // global configuration setting
  //
  router.configure({
    on —allroutes
  });
  router.init();

...or if you're developing a site that doesn't really look like a SPA, you can completely discard client-side routing and use your usual express routing:

var express=require('express')
var app=express()
app.get('/script1/', function(req,res){
  varscript1 = require('script1');
  // Use script1 here. Use functions, etc.
  res.send(script1.hoge);
});

router.on(/script2/, function(){
  // Now we're going to use script2, and we're going to use functions, etc.
  res.send(script2.hoge);
});


app.listen (3000)

[browserify] for application code unification(e.g. http://browserify.org/) ) and so on, it's easy to do after implementation.Compression, etc. [uglify](See additional tools such as http://lisperator.net/uglifyjs/) .


2022-09-30 11:30

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.