简体   繁体   中英

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.
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 . 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?
  • Should I be using a different syntax: const express = require('express'); & const app = express(); instead of var express = require('express'); & var app = express();

``` or this

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

``` Instead of the first block of code above.

Thanks! :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.

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. 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. 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.

what's the difference between using res.render and 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.send() just sends the raw content you give it.

Should I be using a different syntax: const express = require('express'); & const app = express(); instead of var express = require('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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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