简体   繁体   English

使用express.js中的路由服务静态文件

[英]Serve static files using routes in express.js

I have a bunch of html files that I want to serve as static files. 我有一堆要用作静态文件的html文件。 However, I need the routes to not have any .html in it. 但是,我需要路由中没有任何.html。

When the route is example.com/about it should serve about.html 当路线为example.com/about时,则应提供about.html

The research I did on serving static files seems to suggest that it is not entirely possible. 我对提供静态文件所做的研究似乎表明这并非完全可能。 Is that right, or am I missing something? 是这样吗,还是我想念什么?

You need to serve your static file using the router 您需要使用路由器提供静态文件

app.get('/about', function(req, res) {
    res.render('about.html');
});

Before that you should setup a middleware for the html rendering engine 在此之前,您应该为html呈现引擎设置一个中间件

Or if you have a lot of static files, you could use a middleware to do it for you 或者,如果您有很多静态文件,则可以使用中间件为您完成

https://www.npmjs.com/package/clean-urls https://www.npmjs.com/package/clean-urls

You can create a short little piece of middleware that will process whichever preset names you configure it for. 您可以创建一个简短的中间件,该中间件将处理您为其配置的任何预设名称。 Anything not in that list will go to your normal routes: 该列表中未包含的所有内容都会进入您的常规路线:

// create static file lookup table for the desired names
var staticFiles = ["about", "index", "home"].reduce(function(obj, item) {
    obj["/" + item] = true;
    return obj;
}, {});

app.use(function(req, res, next) {
    // if the path is found in the lookup table, then
    // add ".html" onto the end and get that file from the base directory
    // of you could use any source directory for those files
    if (staticFiles[req.path]) {
        res.sendFile(path.join(__dirname, req.path + ".html"));
        return;
    }
    next();
});

Note: this very carefully only serves specific files so nobody can go snooping around in your file system with other types of URLs. 注意:这非常谨慎,仅提供特定文件,因此没有人可以使用其他类型的URL在您的文件系统中窥探。


Here's an example where the files are served from a subdirectory below our base directory named "html": 这是一个示例,其中文件是从名为“ html”的基本目录下的子目录提供的:

app.use(function(req, res, next) {
    if (staticFiles[req.path]) {
        res.sendFile(path.join(__dirname, "html", req.path + ".html"));
        return;
    }
    next();
});

In node v0.12+, you can use the Set object for your list of static routes like this: 在节点v0.12 +中,可以将Set对象用于静态路由列表,如下所示:

var staticFiles = new Set(["/about", "/index", "/home"]);

app.use(function(req, res, next) {
    if (staticFiles.has(req.path)) {
        res.sendFile(path.join(__dirname, "html", req.path + ".html"));
        return;
    }
    next();
});

If you have a lot of static pages, the ideal way would be to put them up in a single route, lets call it static.js . 如果您有很多静态页面,理想的方法是将它们放在一条路由中,将其static.js It can be something like, 可能是这样的,

module.exports = function () {
var router = require('express').Router();

var pages = {
    'about-us': 'path/to/about-us',
    'contact-us': 'path/to/contact-us'
};

router.get('/:page', function (req, res, next) {
    if (! pages[req.params.page]) {
        return next();
    }

    res.render(pages[req.params.page]);
});

return router;
};

Attach this route to the app, app.use(require('./static')()); 将此路由附加到应用程序app.use(require('./static')()); and you are good to go. 而且你很好。

Try this. 尝试这个。

app.use(express.static(path.join(__dirname, 'xyz')));

where xyz is the folder you are trying to make available as static. 其中xyz是您要使其变为静态的文件夹。 The folder name is relative to the application root. 文件夹名称是相对于应用程序根目录的。

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

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