Javascript 使用express.js中的路由服务静态文件
我有一堆html文件,我想用作静态文件。但是,我需要路由中不包含任何.html 当路线是 example.com/about 它应该提供关于.html的服务 我在提供静态文件方面所做的研究似乎表明,这并非完全可能。是这样,还是我遗漏了什么?试试这个Javascript 使用express.js中的路由服务静态文件,javascript,express,Javascript,Express,我有一堆html文件,我想用作静态文件。但是,我需要路由中不包含任何.html 当路线是 example.com/about 它应该提供关于.html的服务 我在提供静态文件方面所做的研究似乎表明,这并非完全可能。是这样,还是我遗漏了什么?试试这个 app.use(express.static(path.join(__dirname, 'xyz'))); 其中xyz是您试图使其作为静态文件夹可用的文件夹。文件夹名称与应用程序根目录相对。试试这个 app.use(express.static(p
app.use(express.static(path.join(__dirname, 'xyz')));
其中xyz是您试图使其作为静态文件夹可用的文件夹。文件夹名称与应用程序根目录相对。试试这个
app.use(express.static(path.join(__dirname, 'xyz')));
其中xyz是您试图使其作为静态文件夹可用的文件夹。文件夹名称与应用程序根相对。您需要使用路由器为静态文件提供服务
app.get('/about', function(req, res) {
res.render('about.html');
});
在此之前,您应该为html呈现引擎设置一个中间件
或者,如果您有很多静态文件,您可以使用中间件来完成
您需要使用路由器提供静态文件
app.get('/about', function(req, res) {
res.render('about.html');
});
在此之前,您应该为html呈现引擎设置一个中间件
或者,如果您有很多静态文件,您可以使用中间件来完成
您可以创建一小段中间件,用于处理您为其配置的预设名称。不在该列表中的任何内容都将转到您的正常路线:
// 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();
});
注意:这非常小心地只服务于特定的文件,所以没有人可以在您的文件系统中窥探其他类型的URL
下面是一个示例,其中文件是从我们的基本目录下名为“html”的子目录提供的:
在节点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();
});
您可以创建一小段中间件来处理您为其配置的预设名称。不在该列表中的任何内容都将转到您的正常路线:
// 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();
});
注意:这非常小心地只服务于特定的文件,所以没有人可以在您的文件系统中窥探其他类型的URL
下面是一个示例,其中文件是从我们的基本目录下名为“html”的子目录提供的:
在节点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();
});
如果您有很多静态页面,理想的方法是将它们放在一个路由中,我们称之为
static.js
。它可以是这样的
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;
};
将此路由附加到应用程序,
app.use(require('./static')()代码>你可以走了 如果您有很多静态页面,理想的方法是将它们放在一个路由中,我们称之为static.js
。它可以是这样的
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;
};
将此路由附加到应用程序,app.use(require('./static')()代码>你可以走了 这将不起作用,因为它将路由指向about而不是about.htmlTime是正确的。例如,您应该尝试其他类似的中间件。这将不起作用,因为它将路由指向about而不是about.htmlTime是正确的。例如,您应该尝试其他类似的中间件。这将不起作用,因为它将路由指向about而不是about.htmlTime是正确的。例如,您应该尝试其他类似的中间件。