@Vit632

Как перейти на другую страницу ejs?

Не получается перейти на другую страницу ejs.

Index.ejs
<h1>Index page</h1>

<a href="/about" >About-1. Описание</a> </br>
<a href="http://localhost:3000/About/" >About-2. Описание</a>

В файл routes.js добавил.
.get('/about', (req, res) => {
       res.render('about');
      })


routes.js Полный код.
const multer = require('multer');
const rand = require('randomstring');

const filesStorage = multer.diskStorage({
  destination: (req, file, next) => {
    next(null, 'static/uploads/files');
  },
  filename: (req, file, next) => {
    const ext = file.originalname.split('.').pop();
    next(null, rand.generate({
      length: 32,
      charset: 'alphabetic'
    }) + '.' + ext);
  }
});
const filesUpload = new multer({
  storage: filesStorage
});


const site = {
  main: require('./controllers/main')
};

const cms = {
  articles: require('./controllers/cms/articles'),
  files: require('./controllers/cms/files'),
  lang: require('./controllers/cms/lang'),
  slideshow: require('./controllers/cms/slideshow')
};

module.exports = (app, passport) => {

  app
    .get('/', site.main.lang)
    .get('/video', site.main.video)
    .get('/slideshow', site.main.slideshow)
    .get('/:lang', site.main.index)
    
    /*articles*/        
    .get('/:lang/articles', site.main.index)
    .get('/:lang/articles/:id', site.main.article)

    .get('/:lang/panomuseum', site.main.panomuseum)
    .get('/:lang/panomuseum/2', site.main.panomuseum2)
    .get('/:lang/panotheatre', site.main.panotheatre)
    
    /*My*/
    // .get('/:lang/articles', site.main.index)
    .get('/Index', site.main.index)
     .get('/history', site.main.history)
    // .get('/history', (req, res) => {
    //   res.render('history');
    //  })
     .get('/about', (req, res) => {
       res.render('about');
      })
    ;


  app

    .get('/cms/lang', cms.lang.index)
    .post('/cms/lang', filesUpload.any(), cms.lang.save)

    .get('/cms/:lang/articles', cms.articles.index)
    .post('/cms/articles/saveOrder', cms.articles.saveOrder)

    .get('/cms/:lang/articles/add', cms.articles.add)
    .post('/cms/:lang/articles/add', filesUpload.any(), cms.articles.postAdd)

    .get('/cms/:lang/articles/:id/edit', cms.articles.edit)
    .post('/cms/:lang/articles/:id/edit', filesUpload.any(), cms.articles.postEdit)
    .get('/cms/:lang/articles/:id/delete', cms.articles.delete)

    .get('/cms/:lang/articles/:id', cms.articles.subArticle)
    .get('/cms/:lang/articles/add/:id', cms.articles.add)

    .post('/cms/files/delete', cms.files.delete)
    .post('/cms/files/saveFile', filesUpload.single('file'), cms.files.saveFile)
    .post('/cms/files/saveThumb', filesUpload.single('thumb'), cms.files.saveThumb)

    .get('/cms/slideshow', cms.slideshow.index)
    .post('/cms/slideshow/save', filesUpload.any(), cms.slideshow.save);

  return app;


62f42e2e575ed655480861.png
62f42e3bc4115870851077.png

Обновление-1 --- --- ---
Моя цель: адаптировать существующий проект под свой проект.

controllers\main.js
spoiler
const db = require('../db');
const fs = require('fs');
const path = require('path');
const config = require('../config.js');
class Main {

  async video(req, res, next) {
    const videoFolder = './static/video'
    let videos = []

    fs.readdirSync(videoFolder).forEach((file) => {
      let extension = path.extname(file)
      let filename = path.basename(file, extension)

      videos.push({
        file,
        filename: parseInt(filename),
      })
    })

    videos = videos.sort((a, b) => {
      return a.filename - b.filename
    })



    return res.render('video', {
      domain: config.express.domain,
      videos,
    })
  }

    async panomuseum(req, res) {
    const article = await db.article.getByID(req.params.lang);
    const sub = await db.article.getRoot(req.params.lang);
    const files = await db.files.getByOwnerId(req.params.lang);

    const lang = await db.lang.getById(req.params.lang);

    return res.render('panomuseum', {
      article,
      sub,
      files,
      lang
    });
  }

  async panomuseum2(req, res) {
    const article = await db.article.getByID(req.params.lang);
    const sub = await db.article.getRoot(req.params.lang);
    const files = await db.files.getByOwnerId(req.params.lang);

    const lang = await db.lang.getById(req.params.lang);

    return res.render('panomuseum2', {
      article,
      sub,
      files,
      lang
    });
  }

  async panotheatre(req, res) {
    const article = await db.article.getByID(req.params.lang);
    const sub = await db.article.getRoot(req.params.lang);
    const files = await db.files.getByOwnerId(req.params.lang);

    const lang = await db.lang.getById(req.params.lang);

    return res.render('panotheatre', {
      article,
      sub,
      files,
      lang
    });
  }

  async index(req, res) {
    const article = await db.article.getByID(req.params.lang);
    const sub = await db.article.getRoot(req.params.lang);
    const files = await db.files.getByOwnerId(req.params.lang);


    const lang = await db.lang.getById(req.params.lang);
    const timeout = await db.settings.getByID("timeout");
    const caption = await db.settings.getByID("caption");


    return res.render("index", {
      article,
      sub,
      files,
      lang,
      timeout,
      caption,
      domain: req.app.get("domain"),
    });
  }

  // async history(req, res) {
  //   // const article = await db.article.getByID(req.params.lang);
  //   // const sub = await db.article.getRoot(req.params.lang);
  //   // const files = await db.files.getByOwnerId(req.params.lang);


  //   // const lang = await db.lang.getById(req.params.lang);
  //   // const timeout = await db.settings.getByID("timeout");
  //   // const caption = await db.settings.getByID("caption");


  //   return res.render("history", {   
  //     domain: req.app.get("domain")
  //   });
  // }


  async history(req, res) {
    console.log('Request for history page recieved');
    return res.render("history");
  }

  async about(req, res) {
    console.log('Request for about page recieved');
    return res.render("about");
  }

  async menu(req, res) {
    return res.render("menu", {
      domain: req.app.get("domain"),
    });
  }

  async slideshow(req, res) {
    const slideshow = await db.files.getSlideshow();
    const timer = await db.settings.getByID("timer");

    return res.render("slideshow", {
      slideshow,
      timer,
      domain: req.app.get("domain"),
    });
  }

  async slide(req, res) {
    const slideshow = await db.files.getByID(req.params.id);
    const timer = await db.settings.getByID("timer");

    return res.render("slideshow", {
      slideshow: [slideshow],
      timer,
      domain: req.app.get("domain"),
    });
  }

  async article(req, res) {
    const article = await db.article.getByID(req.params.id);
    const sub = await db.article.getSub(req.params.id);
    const files = await db.files.getByOwnerId(req.params.id);
    const id = req.params.id;
    const lang = await db.lang.getById(req.params.lang);
    const timeout = await db.settings.getByID("timeout");
    const caption = await db.settings.getByID("caption");

    return res.render("index", {
      id,
      article,
      sub,
      files,
      lang,
      timeout,
      caption,
      domain: req.app.get("domain"),
    });
  }

  
  async lang(req, res) {
    const langs = await db.lang.getAll();

    let activeCount = 0;

    for (let lang of langs) {
      if (lang.value == 1) {
        activeCount++;
      }
    }

    if (activeCount == 0) {
      return res.redirect("/0");
    } else if (activeCount == 1) {
      for (let lang of langs) {
        if (lang.value == 1) {
          return res.redirect("/" + lang.id);
        }
      }
    }

    const timeout = await db.settings.getByID("timeout");

    return res.render("lang", {
      langs,
      timeout,
    });
  }

  async openSlide(req, res) {
    console.log("openSlide");
    let files = await db.files.getSyncSmartHome();

    parentIO.sockets.in("client").emit("goToUrl", {
      message: "/slide/" + files[parseInt(req.params.id)].id,
    });

    return res.json({
      success: true,
    });
  }

  async openSlideshow(req, res) {
    console.log("open slideshow");

    parentIO.sockets.in("client").emit("goToUrl", {
      message: "/slideshow",
    });

    return res.json({
      success: true,
    });
  }
}
module.exports = new Main();



62f4877c8a3f9501546410.png
  • Вопрос задан
  • 33 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы