router.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const path = require('path');
  2. const prefix = '../page/';
  3. const route = {
  4. dashboard: 'dashboard/dashboard.html',
  5. edit: 'edit/edit.html'
  6. }
  7. class RouterService {
  8. path(dest, params) {
  9. // Add GET params
  10. let paramsQuery = '';
  11. if (params) {
  12. let first = true;
  13. for (const key in params) {
  14. paramsQuery += first ? '?' : '&';
  15. paramsQuery += `${key}=${params[key]}`;
  16. first = false;
  17. }
  18. }
  19. // Get path to the file
  20. if (route[dest]) {
  21. const filepath = path.join(__dirname, prefix, route[dest]);
  22. if (filepath[filepath.length - 1] === '/') {
  23. filepath = filepath.substring(0, filepath.length - 1);
  24. }
  25. return filepath + paramsQuery;
  26. }
  27. return false;
  28. }
  29. redirect(dest, params) {
  30. document.location = this.path(dest, params);
  31. }
  32. reload() {
  33. document.location.reload();
  34. }
  35. }
  36. module.exports = new RouterService();