简体   繁体   English

在node.js中设置很多路由

[英]setting up a lot of routes in node.js

I've created a webpage to use it locally as a way to save information about random topics and college stuff. 我创建了一个网页,以在本地使用它作为保存有关随机主题和大学课程的信息的方法。 I have a ton of routes like the ones shown below: 我有很多路线,如下所示:

//app.js - using node and express
    app.get('/page1', function(req, res){
        res.render('page1');
    });
    app.get('/page2', function(req, res){
        res.sendFile('views/page2.html', { root: __dirname });
    });

Each one of these routes has a .ejs file or a .html file, and they are all quite small. 这些路由中的每一个都有一个.ejs文件或.html文件,它们都非常小。
I don't think I've made any major mistakes, but I gotta a feeling I'm not using "best practices", or at least could be doing some differently. 我认为我没有犯任何重大错误,但是我有一种感觉,我没有使用“最佳实践”,或者至少可以做一些不同的事情。

  • Is there anything wrong with using a lot a routes like this? 使用很多这样的路线有什么问题吗? Should change something to optimize the rendering of my pages? 应该更改一些内容以优化页面的呈现吗?
  • As I said before, I'm using .ejs on most of the pages, and most of them have the same header.ejs and footer.ejs . 如前所述,我在大多数页面上使用.ejs ,并且大多数页面具有相同的header.ejsfooter.ejs Every time I change pages that have the same header/footer, do they get loaded again, or since they are using the same header/footer files the server only requests the content in between? 每次我更改具有相同页眉/页脚的页面时,是否会再次加载它们,或者由于它们使用的是相同的页眉/页脚文件,服务器仅请求两者之间的内容?
  • what's the difference between using res.render and res.send? 使用res.render和res.send有什么区别?
  • Should I be using a different syntax: const express = require('express'); 我应该使用其他语法const express = require('express');const express = require('express'); & const app = express(); const app = express(); instead of var express = require('express'); 而不是var express = require('express'); & var app = express(); var app = express();

``` or this ``或这个

app.get('/page1', (req, res) => {
    res.render('page1');
});

``` Instead of the first block of code above. 而不是上面的第一段代码。

Thanks! 谢谢! :D :d

Is there anything wrong with using a lot a routes like this? 使用很多这样的路线有什么问题吗? Should change something to optimize the rendering of my pages? 应该更改一些内容以优化页面的呈现吗?

Nothing technically wrong with it. 技术上没有错。 Many plain res.sendFile() routes can probably be replaced with a single express.static() middleware statement to simplify your code. 许多简单的res.sendFile()路由可能都可以用一个express.static()中间件语句代替,以简化您的代码。

Lots of res.render() routes that don't pass any customized data to EJS can also probably be replaced by a single middleware that handles either a whole directory of template files (and their corresponding routes) or a list of files. 许多没有将任何自定义数据传递给EJS的res.render()路由也可能由一个单独的中间件代替,该中间件处理模板文件的整个目录(及其对应的路由)或文件列表。 That would be a lot more DRY than spelling out each route separately. 这比单独阐明每条路线要耗费更多的精力

As I said before, I'm using .ejs on most of the pages, and most of them have the same header.ejs and footer.ejs. 如前所述,我在大多数页面上都使用.ejs,并且大多数页面具有相同的header.ejs和footer.ejs。 Every time I change pages that have the same header/footer, do they get loaded again, or since they are using the same header/footer files the server only requests the content in between? 每次我更改具有相同页眉/页脚的页面时,是否会再次加载它们,或者由于它们使用的是相同的页眉/页脚文件,服务器仅请求两者之间的内容?

EJS caches templates in memory (unless you disable caching) so the header and footer templates won't get loaded over and over again from disk. EJS将模板缓存在内存中(除非您禁用缓存),因此页眉和页脚模板不会一次又一次地从磁盘加载。

what's the difference between using res.render and res.send? 使用res.render和res.send有什么区别?

These are fully covered in the documentation. 这些已在文档中全面介绍。 In a nutshell, res.render() supports your template engine and local data to feed to the template engine to allow it to add data to the template. 简而言之, res.render()支持您的模板引擎和本地数据以馈入模板引擎,以允许其将数据添加到模板。 res.send() just sends the raw content you give it. res.send()只是发送您给它的原始内容。

Should I be using a different syntax: const express = require('express'); 我应该使用其他语法吗:const express = require('express'); & const app = express(); &const app = express(); instead of var express = require('express'); 而不是var express = require('express'); & var app = express(); &var app = express();

It is considered a good practice to use const whenever the variable you are declaring should get its initial value and not be assigned to again. 每当您声明的变量应获得其初始值且不再被分配时,最好使用const In addition to some coding safety, this also can sometimes allow the interpreter to do more optimizations when using the variable (since its value can't be changed). 除了某些编码安全性之外,有时还可以使解释器在使用变量时进行更多优化(因为不能更改其值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM