I've created a basic web app using Express, and provided a dynamic route such as:
我使用Express创建了一个基本的Web应用程序,并提供了一个动态路由,例如:
app.get('/myroute/:user_id', function (req, res) {
res.render('mytemplate');
});
This renders the EJS template "mytemplate.ejs" when the user visits /myroute/15
当用户访问/ myroute / 15时,这将呈现EJS模板“mytemplate.ejs”
Now, I'd also like to create a second route /staticroute
that transmits a static HTML page somepage.html
.
现在,我还想创建第二个路由/ staticroute,它传输静态HTML页面somepage.html。
What's the best way to do this? Should these static pages be served outside of Express? I've seen some related answers on *, but they either use deprecated functionality or they do not support cacheing.
最好的方法是什么?这些静态页面应该在Express之外提供吗?我在*上看到了一些相关的答案,但它们要么使用已弃用的功能,要么不支持缓存。
2 个解决方案
#1
2
There's res.sendfile()
for such cases. Although if you don't need special routes for static files, you might look into using the express.static
middleware instead.
这种情况有res.sendfile()。虽然如果您不需要静态文件的特殊路由,您可能会考虑使用express.static中间件。
#2
1
Here's a quick solution you can try with memcached (too long to put into comments, I'd still recommend nginx's proxy_cache to avoid coding this below):
这是一个快速的解决方案,您可以尝试使用memcached(太长时间不能发表评论,我仍然建议使用nginx的proxy_cache以避免在下面进行编码):
var Memcached = require('memcached'); // sudo apt-get install memcached
// npm install memcached
var memcached = new Memcached('localhost:11211'); // default
var ttl = 86400; // 24hrs time to live
function readContent(callback) { // async read with callback
fs.readFile("./someIndex.html", function (err, content) {
(err) ? callback(err) : callback(null, content);
})
}
app.get('/someRoutes', function (req, res) {
memcached.get('foo', function( err, content){ // get 'foo' from memcached
if( err ) console.error( err ); // .get() error handling
if( content ) { // data in memcached
// render content and output
} else { // data not in memcached
readContent(function (err, myFile) { // try reading file
if (myFile) { // file found, save to memcached & render
memcached.set('foo', myFile, ttl, function( err, data ){
if( err ) console.error( err ); // .set() error handling
});
// render myFile and output
}
}); // end readContent()
} // end if-content-else-
}); // end memcached.get()
}); // end app.get
#1
2
There's res.sendfile()
for such cases. Although if you don't need special routes for static files, you might look into using the express.static
middleware instead.
这种情况有res.sendfile()。虽然如果您不需要静态文件的特殊路由,您可能会考虑使用express.static中间件。
#2
1
Here's a quick solution you can try with memcached (too long to put into comments, I'd still recommend nginx's proxy_cache to avoid coding this below):
这是一个快速的解决方案,您可以尝试使用memcached(太长时间不能发表评论,我仍然建议使用nginx的proxy_cache以避免在下面进行编码):
var Memcached = require('memcached'); // sudo apt-get install memcached
// npm install memcached
var memcached = new Memcached('localhost:11211'); // default
var ttl = 86400; // 24hrs time to live
function readContent(callback) { // async read with callback
fs.readFile("./someIndex.html", function (err, content) {
(err) ? callback(err) : callback(null, content);
})
}
app.get('/someRoutes', function (req, res) {
memcached.get('foo', function( err, content){ // get 'foo' from memcached
if( err ) console.error( err ); // .get() error handling
if( content ) { // data in memcached
// render content and output
} else { // data not in memcached
readContent(function (err, myFile) { // try reading file
if (myFile) { // file found, save to memcached & render
memcached.set('foo', myFile, ttl, function( err, data ){
if( err ) console.error( err ); // .set() error handling
});
// render myFile and output
}
}); // end readContent()
} // end if-content-else-
}); // end memcached.get()
}); // end app.get